英文:
Null object when setting the value
问题
String tempWithDegree = weatherDays.get(position).getTempWithDegree();
holder.textViewTemp.setText(tempWithDegree);
英文:
I make a request, get a list and make a weather forecast out of it. On request, it comes to a variable, but when setting the value, I get an error. Help find the problem.
The problem is in the line - String tempWithDegree = weatherDays.get(position).getTempWithDegree();
holder.textViewTemp.setText(tempWithDegree);
Debug shows that the variable gets value
Class WeatherDay:
public class WeatherDay {
public class WeatherTemp {
Double temp;
Double temp_min;
Double temp_max;
}
public class WeatherDescription {
String icon;
}
@SerializedName("main")
private WeatherTemp temp;
@SerializedName("weather")
private List<WeatherDescription> descriptions;
@SerializedName("name")
private String city;
@SerializedName("dt")
private long timestamp;
public WeatherDay(WeatherTemp temp, List<WeatherDescription> descriptions) {
this.temp = temp;
this.descriptions = descriptions;
}
public Calendar getDate() {
Calendar date = Calendar.getInstance();
date.setTimeInMillis(timestamp * 1000);
return date;
}
public String getTemp() { return String.valueOf(temp.temp); }
public String getTempMin() { return String.valueOf(temp.temp_min); }
public String getTempMax() { return String.valueOf(temp.temp_max); }
public String getTempInteger() { return String.valueOf(temp.temp.intValue()); }
public String getTempWithDegree() { return (temp.temp.intValue()) + "\u00B0"; }
public String getCity() { return city; }
public String getIcon() { return descriptions.get(0).icon; }
public String getIconUrl() {
return "http://openweathermap.org/img/w/" + descriptions.get(0).icon + ".png";
}
Class Forecast:
@SerializedName("list")
private List<WeatherDay> items;
public WeatherForecast(List<WeatherDay> items) {
this.items = items;
}
public List<WeatherDay> getItems() {
return items;
}
Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
My Adapter Class:
public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ForecastHolder>{
private List<WeatherDay> weatherDays = new ArrayList<>();
@NonNull
@Override
public ForecastHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.weather_item, parent, false);
return new ForecastHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ForecastHolder holder, int position) {
SimpleDateFormat formatDayOfWeek = new SimpleDateFormat("E");
if (weatherDays.get(position).getDate().get(Calendar.HOUR_OF_DAY) == 15) {
String dayOfWeek = formatDayOfWeek.format(weatherDays.get(position).getDate().getTime());
holder.textViewDay.setText(dayOfWeek);
// String tempWithDegree = weatherDays.get(position).getTempWithDegree();
// holder.textViewTemp.setText(tempWithDegree);
Picasso.get().load(weatherDays.get(position).getIconUrl()).into(holder.imageView);
}
}
@Override
public int getItemCount() {
return weatherDays.size();
}
public void setWeatherDays(List<WeatherDay> days) {
weatherDays.addAll(days);
notifyDataSetChanged();
}
public void clearForecasts() {
weatherDays.clear();
notifyDataSetChanged();
}
public class ForecastHolder extends RecyclerView.ViewHolder {
private TextView textViewTemp;
private TextView textViewDay;
private ImageView imageView;
public ForecastHolder(@NonNull View itemView) {
super(itemView);
textViewTemp = itemView.findViewById(R.id.txtViewTemp);
imageView = itemView.findViewById(R.id.imageView);
textViewDay = itemView.findViewById(R.id.txtViewDay);
}
}
}
Thank for u help!
答案1
得分: 3
以下是您要翻译的内容:
异常是因为在holder.textViewTemp.setText(...)
中,textViewTemp为空。
在onCreateViewHolder
方法中,您没有为视图持有者对象设置textViewTemp
的值。如果看起来您已经设置了,考虑到查找视图可能失败的可能性。也许您使用的布局没有具有您正在使用的ID的视图。
英文:
The exception was thrown because textViewTemp is null in holder.textViewTemp.setText(...)
.
You are not setting a value for textViewTemp
in the view holder object in the onCreateViewHolder
method. If it looks like you are, consider the possibility that looking up the view failed. Perhaps the layout you are using does not have a view with the ID you are using.
答案2
得分: 0
问题不在于值。而在于 textViewTemp
。您还没有对其进行初始化。如果您分享您的适配器类,我们可以帮助您。
英文:
The problem isn't about value. It's about textViewTemp
. You haven't initialized it. If you share your adapter class, we can help you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论