我得到java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.mycompany.alawamhm.hellobutton.Comic.toString()' on a null object reference
但我不确定为什么漫画在这里是空的。我假设comic = om.readValue(new URL(urlStrings[0]),Comic.class);
没有做我想做的事。
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private int mIssueNumber = 1;
private TextView mMessageTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMessageTextView = (TextView)findViewById(R.id.message_text);
Button incrementButton = (Button)findViewById(R.id.button);
incrementButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mIssueNumber++;
String urlString = String.format(Locale.US, "http://xkcd.com/%d/info.0.json", mIssueNumber);
new GetComicTask().execute((urlString));
}
});
}
public class GetComicTask extends AsyncTask<String, Void, Comic> {
@Override
protected Comic doInBackground(String... urlStrings) {
Comic comic = null;
ObjectMapper om = new ObjectMapper();
try {
comic = om.readValue(new URL(urlStrings[0]),Comic.class);
} catch (IOException e) {
e.getMessage();
}
return comic;
}
@Override
protected void onPostExecute(Comic comic) {
super.onPostExecute(comic);
mMessageTextView.setText(comic.toString());
}
}
}
更新的问题 漫画类:
public class Comic {
private int num;
private int month;
private int day;
private int year;
private String link;
private String news;
private String transcript;
private String safe_title;
private String alt;
private String img;
private String title;
public Comic() {
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getNews() {
return news;
}
public void setNews(String news) {
this.news = news;
}
public String getTranscript() {
return transcript;
}
public void setTranscript(String transcript) {
this.transcript = transcript;
}
public String getSafe_title() {
return safe_title;
}
public void setSafe_title(String safe_title) {
this.safe_title = safe_title;
}
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Comic{" +
"num=" + num +
", month=" + month +
", day=" + day +
", year=" + year +
", link='" + link + '\'' +
", news='" + news + '\'' +
", transcript='" + transcript + '\'' +
", safe_title='" + safe_title + '\'' +
", alt='" + alt + '\'' +
", img='" + img + '\'' +
", title='" + title + '\'' +
'}';
}
}
答案 0 :(得分:0)
问题是你在try-catch块之外返回comic
。您正在进行网络调用,因此它肯定会在try或catch块中消耗一些时间,但是您在该块之外进行回退,因此该方法将立即返回null comic
实例,而无需等待try-catch的完成块。您可以通过以下方式修改代码。
public class GetComicTask extends AsyncTask<String, Void, Comic> {
@Override
protected Comic doInBackground(String... urlStrings) {
Comic comic = null;
ObjectMapper om = new ObjectMapper();
try {
comic = om.readValue(new URL(urlStrings[0]),Comic.class);
return comic;
} catch (IOException e) {
e.getMessage();
return null;
}
}
@Override
protected void onPostExecute(Comic comic) {
super.onPostExecute(comic);
mMessageTextView.setText(comic.toString());
}
}
通过这种方式,只有在发生异常时才会获得null。 我希望这能解决你的问题。