user547995
user547995

Reputation: 2085

Unknown NullPointerException

Hy!

I don't find the error. The error should be at the btlogin.setOnClickListener

Login Class

package android.skiptvad;

public class Login extends Activity{
 ProgressDialog pd = null;
 //final SharedPreferences settings = getSharedPreferences("pref", 0);
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.login);
  final SharedPreferences settings = getSharedPreferences("pref", 0);
  final EditText user = (EditText)findViewById(R.id.login_etuser);
  final EditText pw = (EditText)findViewById(R.id.login_etpw);
  Button btlogin = (Button)findViewById(R.id.login_btlog);
  Button btnew = (Button)findViewById(R.id.login_btnew);
  final CheckBox cb = (CheckBox)findViewById(R.id.login_cb);

  if (settings.getString("user", "") != "")
  {
   user.setText(settings.getString("user", ""));
   pw.setText(settings.getString("pw", ""));
  }



  btlogin.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    if (!user.getText().toString().equals("") && !pw.getText().toString().equals(""))
    {
     pd.setCancelable(true);
     pd = ProgressDialog.show(Login.this,"","Loading. Please wait...", true);
     List<NameValuePair> params = new ArrayList<NameValuePair>();
                   params.add(new BasicNameValuePair("username", user.getText().toString()));
                   params.add(new BasicNameValuePair("password", pw.getText().toString()));
                   Handler handler = new Handler(){
                   @Override
                   public void handleMessage(Message msg) {
                    pd.cancel();
                      if (msg.obj.toString()!= null)
                      {
                     JSONParse json = null;
                     try {
        json = new JSONParse(msg.obj.toString());
      } catch (JSONException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      String session = json.getsessionid();
      SharedPreferences.Editor editor = settings.edit();
                       editor.putString("session_id", session);
                       editor.commit();
                       finish();

                      }
                      else
                      {

                       runOnUiThread(new  Runnable() {
       public void run() {
        Toast toast ;
                toast =  Toast.makeText(getApplicationContext(), "error", 500);
            toast.show();
       }
      });
                      }
                   }
                   };
     HttpConnection con = new HttpConnection(params, "http://surfkid.redio.de/login", handler);
     Log.e("xxxcon", "ok");
     con.start();
    }
    if (cb.isChecked())
    {
   SharedPreferences.Editor editor = settings.edit();
                         editor.putString("user", user.getText().toString());
                         editor.putString("pw", pw.getText().toString());
                         editor.commit();
    }

   }
  });
  btnew.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
     startActivityForResult(new Intent(Login.this, NewAccount.class), 1);

   }
  });
 }


}

LogCat:

01-28 14:11:03.180: ERROR/AndroidRuntime(12132): FATAL EXCEPTION: main
01-28 14:11:03.180: ERROR/AndroidRuntime(12132): java.lang.NullPointerException
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at android.skiptvad.Login$1.onClick(Login.java:71)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at android.view.View.performClick(View.java:2461)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at android.view.View$PerformClick.run(View.java:8888)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at android.os.Handler.handleCallback(Handler.java:587)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at android.os.Looper.loop(Looper.java:123)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at java.lang.reflect.Method.invokeNative(Native Method)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at java.lang.reflect.Method.invoke(Method.java:521)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-28 14:11:03.180: ERROR/AndroidRuntime(12132):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 388

Answers (2)

dave.c
dave.c

Reputation: 10908

I think @Matt is probably right. Remove the line pd.setCancelable(true); and replace the next line with:

pd = ProgressDialog.show(Login.this,"","Loading. Please wait...", true, true);

This uses a method that accepts boolean cancelable as an input. Here is the documentation

Upvotes: 1

Matt
Matt

Reputation: 3847

I think 'pd' is null when you try to set it as cancelable... It looks like you don't create it until the next line...

 pd.setCancelable(true);
 pd = ProgressDialog.show(Login.this,"","Loading. Please wait...", true);

Upvotes: 1

Related Questions