• Register
0 votes
919 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?

8 4 2
2,300 points

Please log in or register to answer this question.

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.

5 2 1
4,980 points

Related questions

0 votes
0 answers 19 views
19 views
Problem: attempt to invoke virtual method 'int java.util.arraylist.size()' on a null object reference
asked Feb 18 Lucky Guy 1.4k points
0 votes
1 answer 10 views
10 views
Problem: I am trying to save in shared preference and make it display in another activity by getting it again in shared preference but my app crash.b.Attempt to invoke virtual method 'void com.newrelic.agent.android.tracing.TraceMachine. TraceMachine.completeActivityTrace()' on a null object reference
asked Feb 1 Ali 4.4k points
0 votes
1 answer 9 views
9 views
Problem: attempt to invoke interface method 'int java.util.list.size()' on a null object reference
asked 4 days ago Muneeb Saadii 2.4k points
0 votes
1 answer 4 views
4 views
Problem: java.lang.nullpointerexception: attempt to invoke virtual method
asked 4 days ago Hira 2.2k points
0 votes
1 answer 9 views
9 views
Problem: void android.widget.textview.settext(java.lang.charsequence)' on a null object reference
asked 4 days ago Muneeb Saadii 2.4k points
1 vote
1 answer 190 views
190 views
Problem : I am very new to Apex Programming. I am trying to learn Apex using the tutorials available online. I am trying to write Apex Code. I want to set my RecordType.Name in a test class but my class gives me the below message:  "System.NullPointerException: ... : line 45, column 3 External entry point". I am completely stuck with above error. I need help in fixing above Apex related error.
asked May 16, 2020 Martin K 6.6k points
0 votes
1 answer 78 views
78 views
Problem : While executing Android code getting below error java.lang.illegalstateexception could not execute method of the activity
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 139 views
139 views
Problem : I am getting bellow error while learning android and trying to make a semi-useful app. java.lang.illegalstateexception: could not execute method for android:onclick
asked Oct 19, 2019 peterlaw 6.9k points
0 votes
1 answer 10 views
10 views
Problem: public int getRecordsCount() { String countQuery = "SELECT * FROM " + TABLE_LOGIN; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); if(cursor != null && !cursor.isClosed()){ cursor.close(); } / ... with java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM login)
asked Feb 8 ahmedadel9090 2.6k points