Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys am new to android but am having problem connection my android app to sqlServer using JSON..i load the emulator i get these errors when am invoking the authetication function..
07-20 20:55:48.871: D/AndroidRuntime(2082): Shutting down VM
07-20 20:55:48.872: E/AndroidRuntime(2082): FATAL EXCEPTION: main
07-20 20:55:48.872: E/AndroidRuntime(2082): Process: com.example.tropical, PID: 2082
07-20 20:55:48.872: E/AndroidRuntime(2082): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at android.widget.Toast.<init>(Toast.java:101)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at android.widget.Toast.makeText(Toast.java:250)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at com.example.tropical.Login$AsyncLogin.onPreExecute(Login.java:87)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at android.os.AsyncTask.execute(AsyncTask.java:535)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at com.example.tropical.Login$1.onClick(Login.java:49)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at android.view.View.performClick(View.java:4756)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at android.view.View$PerformClick.run(View.java:19749)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at android.os.Handler.handleCallback(Handler.java:739)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at android.os.Handler.dispatchMessage(Handler.java:95)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at android.os.Looper.loop(Looper.java:135)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at android.app.ActivityThread.main(ActivityThread.java:5221)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at java.lang.reflect.Method.invoke(Native Method)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at java.lang.reflect.Method.invoke(Method.java:372)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
07-20 20:55:48.872: E/AndroidRuntime(2082): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
07-20 20:55:51.497: D/gralloc_goldfish(2101): Emulator without GPU emulation detected.
07-20 20:55:59.339: I/Choreographer(2101): Skipped 54 frames!  The application may be doing too much work on its main thread.



And this is my code..
Java
protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		etUserName = (EditText) findViewById(R.id.txtUserID);
		etPassword = (EditText) findViewById(R.id.txtpswd);
        btnLogin = (Button) findViewById(R.id.btnlogin);
		//etUserName =(EditText)findViewById(R.id.)
        btnLogin.setOnClickListener(new View.OnClickListener() {
        	 
            @Override
            public void onClick(View v) {
                  // TODO Auto-generated method stub

                  String username=etUserName.getText().toString();
                  String password=etPassword.getText().toString();

                  // Execute the AsyncLogin class
                  //new AsyncLogin().execute(username,password);
                  AsyncLogin xx = new AsyncLogin();
                  xx.execute(username,password);

            }
      });
	}
	
	protected class AsyncLogin extends AsyncTask<string,> {
		 
        String userName=null;
        @Override
        protected Boolean doInBackground(String... params) {

              RestAPI api = new RestAPI();
        
              boolean userAuth = false;
              try {

                    // Call the User Authentication Method in API
                    JSONObject jsonObj = api.UserAuthetication(params[0],
                                params[1]);

                    //Parse the JSON Object to boolean
                    JSONParser parser = new JSONParser();
                    userAuth = parser.parseUserAuth(jsonObj);
                    userName=params[0];
              } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Log.d("AsyncLogin", e.getMessage());

              }
              return userAuth;

}

code in the JSONObject java class

Java
public JSONObject UserAuthetication(String UserName,String Pin) throws Exception {
       JSONObject result = null;
       JSONObject o = new JSONObject();
       JSONObject p = new JSONObject();
       o.put("interface","RestAPI");
       o.put("method", "UserAuthetication");
       p.put("UserName",mapObject(UserName));
       p.put("Pin",mapObject(Pin));
       o.put("parameters", p);
       String s = o.toString();
       String r = load(s);
       result = new JSONObject(r);
       return result;
   }
Posted
Updated 20-Jul-15 8:30am
v2
Comments
Richard MacCutchan 21-Jul-15 3:34am    
You need to find out where the last call from your code to android OS occurred. That should help to identify which variable is null.

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