• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
12 views

Problem :

I am trying to learn Android programming also I am trying to develop a very basic weather app. My code works perfectly if I try to enter any valid city name. But I want to use the toast and it shows me the error and also to the user when a name is not valid. And I am very worried as this is resulting in the crash, I also tried to remove the toast logic, but still my app is crashing.

Below is the error message :

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) at org.json.JSONTokener.nextValue(JSONTokener.java:94) at org.json.JSONObject.

How can I fix the above error?

by (1.5k points)  

1 Answer

0 votes

Solution :

Today while going through the posts I came across your question.

Android Programming is very easy don’t loose hope.

The solution for your problem is very simple.

It seems the exception is occurring in your doInBackground() and so it is going to return you the null. You can very easily fix this as follows

@Override
protected void onPostExecute(String str) {
    super.onPostExecute(str);
   if(str!=null){
    // You can do you work here on success
  }else{
     // If you get null response or Exception occur 
    }
}

Hope it helps you in resolving your error.

by (3k points)  
...