2017-07-11 2 views
0

Ich bekomme java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.mycompany.alawamhm.hellobutton.Comic.toString()' on a null object reference, aber ich bin mir nicht sicher, warum Comic hier null ist. Ich gehe davon aus, dass comic = om.readValue(new URL(urlStrings[0]),Comic.class); nicht tut, was ich tun will.NullPointerException und nicht sicher, wie zu beheben

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()); 


    } 
    } 
} 

AKTUALISIERT FRAGE Comic Klasse:

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

debug GetComicTask doInBackground Sie erhalten die Antwort – Pavan

+0

verwenden 'e.printStackTrace()' anstelle von 'e.getMessage();'. Es zeigt Ihnen Fehler – ZeroOne

+0

sehr wahrscheinlich 'urlStrings [0]' ist keine gültige URL. Daher schlägt 'neue URL (urlStrings [0])' fehl. Auch sind Sie sicher, 'URL.class' kann zugeordnet werden' Comic.class' – k32y

Antwort

0

Das Problem ist, dass Sie comic außerhalb des Try-Catch-Block zurückkehren. Sie führen einen Netzwerkaufruf aus, so dass es definitiv Zeit in try oder catch benötigt, aber Sie sich außerhalb dieses Blocks zurückversetzen und die Methode comic sofort zurückgibt, ohne auf die Beendigung des try-catch-Blocks zu warten. Sie können den Code folgendermaßen ändern.

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()); 
    } 
    } 

Auf diese Weise erhalten Sie nur eine Null, wenn eine Ausnahme auftritt. Ich hoffe, dass dies Ihr Problem lösen wird.

Verwandte Themen