RuntimeException: Unable to start activity ComponentInfo: java.lang.NullPointerException

6.2k views Asked by At

Hi I am trying display the data from mysql into android listview by using web services axis2. When I run my app getting the following exeption. Here is the Logcat details

12-28 21:20:45.180: E/AndroidRuntime(406): FATAL EXCEPTION: main
12-28 21:20:45.180: E/AndroidRuntime(406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kbj/com.kbj.Invitation}: java.lang.NullPointerException
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.os.Looper.loop(Looper.java:123)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-28 21:20:45.180: E/AndroidRuntime(406):  at java.lang.reflect.Method.invokeNative(Native Method)
12-28 21:20:45.180: E/AndroidRuntime(406):  at java.lang.reflect.Method.invoke(Method.java:521)
12-28 21:20:45.180: E/AndroidRuntime(406):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-28 21:20:45.180: E/AndroidRuntime(406):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-28 21:20:45.180: E/AndroidRuntime(406):  at dalvik.system.NativeStart.main(Native Method)
12-28 21:20:45.180: E/AndroidRuntime(406): Caused by: java.lang.NullPointerException
12-28 21:20:45.180: E/AndroidRuntime(406):  at com.kbj.Invitation.onCreate(Invitation.java:49)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-28 21:20:45.180: E/AndroidRuntime(406):  ... 11 more
12-28 21:20:50.690: I/Process(406): Sending signal. PID: 406 SIG: 9

Here are the my functions

1. retrieveData in Web Services class

public ArrayList<InvitePlayerDetails> retrievePlayer(String userName){

    Connection conn=null;
    PreparedStatement stmt=null;

      ResultSet result = null; 
      String retrievedUserName = "";
      //Boolean b = false;
     // String retrievedPassword = "";
      ArrayList<InvitePlayerDetails> playersList = new ArrayList<InvitePlayerDetails>();

    try{


       conn = Util.getConnection();
       stmt =  conn.prepareStatement("SELECT kbj_username FROM kbj_registration");
       result = stmt.executeQuery();
       System.out.println(result);

       while(result.next()){
        retrievedUserName = result.getString("kbj_username");
        System.out.println(retrievedUserName);
        InvitePlayerDetails p1 = new InvitePlayerDetails(retrievedUserName, false);
       // ArrayList<InvitePlayerDetails> playersList = new ArrayList<InvitePlayerDetails>(retrievedUserName, b);
        playersList.add(p1);

        System.out.println("PlayerListAdded" + playersList);
    }


      }
      catch(SQLException e)
      {
          e.printStackTrace();
      }

      finally
        {           
        Util.CloseDbResources(result, stmt, conn);

        }
return playersList;
}
}

Here is my android activity function..

public class Invitation extends Activity {

private final String NAMESPACE = "http://ws.kbj.com";
private final String URL = "http://10.0.2.2:8085/WS_KBJ/services/Kbj_WebService?wsdl";
private final String SOAP_ACTION = "http://ws.kbj.com/retrievePlayer";
private final String METHOD_NAME = "retrievePlayer";


@Override
protected void onCreate(Bundle savedInstanceState) {


    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.invitation);

    final TextView userName = (TextView) findViewById(R.id.CurPlayername);
    userName.setText(getIntent().getExtras().getString("UserName"));

    final Button fbBtn = (Button) findViewById(R.id.fbButton);
    final Button inviteBtn = (Button) findViewById(R.id.inviteBtn);
    ListView listView = (ListView) findViewById(R.id.invitePlayerList);  
    ArrayList<InvitePlayerDetails> playersList = new ArrayList<InvitePlayerDetails>();  


    playersList = retriveData();


    InvitePlayersBaseAdapter adapter = new InvitePlayersBaseAdapter(this, playersList);  
    listView.setAdapter(adapter);  
    listView.setOnItemClickListener(new OnItemClickListener() {  
        public void onItemClick(AdapterView<?> arg0, View v, int position,  
                long id) {  

        }  
    });  
    final Intent openFbOrTwLoginPage = new Intent(this, FbOrTwLogin.class);
    final Intent openNotificationPage = new Intent(this, Notification.class);
    fbBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(openFbOrTwLoginPage);

        }
    });
    twBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(openFbOrTwLoginPage);

        }
    });
    inviteBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(openNotificationPage);

        }
}

public ArrayList<InvitePlayerDetails> retriveData(){
ArrayList<InvitePlayerDetails> playersList = new ArrayList<InvitePlayerDetails>();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo unameProp =new PropertyInfo();
    unameProp.setName("userName");//Define the variable name in the web service method
    unameProp.setValue(title);//set value for userName variable
    unameProp.setType(String.class);//Define the type of the variable
    request.addProperty(unameProp);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try{
        androidHttpTransport.call(SOAP_ACTION, envelope);
           SoapPrimitive response = (SoapPrimitive)envelope.getResponse();


           System.out.println("SoapPriitive:"+response);
           //status1 = Boolean.parseBoolean(response.toString());

    }
    catch(Exception e){

    }


    return playersList;
}

}

and I have created one pojo class which is

public class InvitePlayerDetails {


private String invitedplayerName;
private boolean ckBox;
  public InvitePlayerDetails(String invitedplayerName,boolean cb1) {  
        this.invitedplayerName = invitedplayerName;  
        this.ckBox = cb1;  
    }  

public String getInvitedplayerName() {
    return invitedplayerName;
}
public void setInvitedplayerName(String invitedplayerName) {
    this.invitedplayerName = invitedplayerName;
}
public boolean getCkBox() {
    return ckBox;
}
public void setCkBox(boolean b) {
    this.ckBox = b;
}

}

Please help me about this.. Tell me where is wrong in my code.. If anyone have code about displaying a data from mysql in listviews using axis2 provide here.. Sorry for my english.. Thanks in advance..

1

There are 1 answers

5
Sam On

You haven't indicate which line is 49, but I have a couple guesses:

userName.setText(getIntent().getExtras().getString("UserName"));

Here you don't check if getIntent(), getExtras(), or even getString("UserName") return null. You should make sure that each method returns valid data before trying to use it.

Also I don't see twBtn anywhere else:

twBtn.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        startActivity(openFbOrTwLoginPage);

    }
});

It is not initialized or even defined...

If neither of these are on line 49, then double check your layout invitation.xml to make sure that every View that you search for with findViewById() is in this layout. Otherwise findViewById() will return null...