【问题标题】:NullPointerException and not sure how to fixNullPointerException 并且不确定如何修复
【发布时间】:2017-12-15 17:00:13
【问题描述】:

我收到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 + '\'' +
            '}';
}

}

【问题讨论】:

  • debug GetComicTask doInBackground 你会得到答案
  • 使用e.printStackTrace() 而不是e.getMessage();。它会显示错误
  • urlStrings[0] 很可能不是有效的 URL。因此new URL(urlStrings[0]) 失败。你也确定URL.class可以映射到Comic.class
  • 当我打印 ulString[0] 时,我得到 link 这是一个有效的 URL,正是我想要的。我按照分步教程进行操作,但我不知道为什么会出现此问题。我不知道它是否可能,但我添加的依赖项或打包选项可能不是最新的。 compile( [group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.4.1'], [group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.4.1'], [group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.4.1'] )
  • 你能粘贴你的Comic.class文件吗?

标签: android json jackson


【解决方案1】:

问题是您在 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。 我希望这能解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-17
    • 2020-04-23
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    相关资源
    最近更新 更多