Click here to Skip to main content
14,828,846 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i write the below code to get data from WebAPI but it through the following errors how can remove this...
and one more thing that which IP address use for URL in genymotion Emulator
>> Caused by: java.lang.NullPointerException
>> at com.example.finalproject.Login$Myclass.doInBackground(Login.java:88)



Java
package com.example.finalproject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
//import java.net.URI;
import java.net.URL;
//import java.net.URLConnection;
import java.nio.charset.MalformedInputException;
//import java.security.PublicKey;

//import android.R.string;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
//import android.util.Log;
//import android.view.Menu;
//import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Login extends Activity {
	
	TextView tvget;
	Button btnget;
	@Override
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		btnget=(Button)findViewById(R.id.btnjson);
		tvget=(TextView)findViewById(R.id.tvjson);
		
		btnget.setOnClickListener(new View.OnClickListener() {@Override
		public void onClick(View v) {
		//	Log.i("Error", "It's working");
			new Myclass().execute("http://192.168.1.201/newAPI/api/user");
		}
			
			
		});
		
	}		
		

	public class Myclass extends AsyncTask<String, String, String>{
	
		
		
		@Override
		protected String doInBackground(String... params) {
			HttpURLConnection connection= null;
			BufferedReader reader = null ;
			
			try {
			URL url=new URL(params[0]);
	connection=(HttpURLConnection)url.openConnection();
	connection.connect();
	InputStream instrm=connection.getInputStream();
	reader=new BufferedReader(new InputStreamReader(instrm));
	StringBuffer b=new StringBuffer() ;
	//String line="";
	while (reader.readLine()!=null) 
	{//line=reader;
		b.append(reader);

	}
	
	return b.toString();
	
	
		} 
			catch (MalformedInputException e) {
			e.printStackTrace();
		}
		
			catch (IOException e) {
		e.printStackTrace();	
		
		}
		finally {
			connection.disconnect();
			try {
				reader.close();
			} catch (IOException e2) {
				
			e2.printStackTrace();
			}
		}
		return null;
		
		
	}	//doinbackground method body end
				
		@Override
		protected void onPostExecute(String result) {
			
			super.onPostExecute(result);
					
			tvget.setText(result);
		}
	}//jsontask class body end
	
	}
Posted
Updated 4-Dec-19 17:24pm

Looking at the error, you're getting a NullPointerException on the following line:
Java
reader.close();

That means the reader object is null, which suggests you have another error being thrown on one of the lines before the reader is created.

Check the reader object for null before trying to call the close method:
Java
if (reader != null) { reader.close(); }

You'll then be able to see the real error, which will let you diagnose and resolve the problem.
   
Comments
Good We 4-Dec-19 23:25pm
   
try {
Log.e("Response",""+response);
JSONObject obj = new JSONObject(response);
JSONArray user_holder = obj.getJSONArray("User_holder");
JSONObject user = user_holder.getJSONObject(0);
user_id=user.getString("id");
user_name=user.getString("user_name");
user_password=user.getString("user_password");
user_emailid=user.getString("user_email");
PreferenceUtils.saveEmail(user_emailid,Register_Page.this);
PreferenceUtils.saveUsername(user_name,Register_Page.this);
PreferenceUtils.savePassword(user_password,Register_Page.this);
PreferenceUtils.saveUserid(user_id, Register_Page.this);
PreferenceUtils.saveLocalValue(localValue, Register_Page.this);
sharedPreferences.writeLginStastu(true);
if(PreferenceUtils.getEmail(Register_Page.this)!=null || !PreferenceUtils.getEmail(Register_Page.this).equals("")){
startActivity(new Intent(Register_Page.this, CongoCoin.class));
sharedPreferences.writeLginStastu(true);
finish();
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("User_json",""+e.toString());
}
Richard Deeming 5-Dec-19 6:46am
   
What does any of this random code have to do with the already-solved question?
Good We 5-Dec-19 0:20am
   
mplementation 'com.intuit.sdpdp-android:1.0.6'
Good We 5-Dec-19 0:20am
   
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'de.hdodenhof:circleimageview:1.2.1'
implementation 'com.facebook.android:facebook-login:4.40.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.android.support:support-media-compat:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.google.android.gms:play-services-auth:9.2.1'
implementation 'com.github.lzyzsd:circleprogress:1.1.0@aar'
implementation 'joda-time:joda-time:2.9.4'
try {
                           Log.e("Response",""+response);
                           JSONObject obj = new JSONObject(response);
                           JSONArray user_holder = obj.getJSONArray("User_holder");
                           JSONObject user = user_holder.getJSONObject(0);
                           user_id=user.getString("id");
                           user_name=user.getString("user_name");
                           user_password=user.getString("user_password");
                           user_emailid=user.getString("user_email");
                           PreferenceUtils.saveEmail(user_emailid,Register_Page.this);
                           PreferenceUtils.saveUsername(user_name,Register_Page.this);
                           PreferenceUtils.savePassword(user_password,Register_Page.this);
                           PreferenceUtils.saveUserid(user_id,  Register_Page.this);
                           PreferenceUtils.saveLocalValue(localValue,  Register_Page.this);
                           sharedPreferences.writeLginStastu(true);
                           if(PreferenceUtils.getEmail(Register_Page.this)!=null || !PreferenceUtils.getEmail(Register_Page.this).equals("")){
                               startActivity(new Intent(Register_Page.this, CongoCoin.class));
                               sharedPreferences.writeLginStastu(true);
                               finish();
                           }
                       } catch (JSONException e) {
                           e.printStackTrace();
                           Log.e("User_json",""+e.toString());
                       }
   
Comments
CHill60 5-Dec-19 4:00am
   
An unexplained code dump is not a good solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900