Reputation: 2826
The below program throws a NullPointerException. In Log cat it shows:
01-09 20:40:34.366: D/RemoteIt(2809): java.lang.NullPointerException
When the button is clicked it doesn't goes to Mousefragment class. I tried to solve it but I can't - how to troubleshoot this?
public class connect extends ListActivity implements OnClickListener{
WifiApManager wifiApManager;
TextView tv;
Button ipscan,con;
ListView lv;
EditText tbIp;
private static final String TAG = "RemoteIt";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.connect);
tv =(TextView) findViewById(R.id.iptv);
ipscan=(Button) findViewById(R.id.scan);
con=(Button) findViewById(R.id.connect);
tbIp=(EditText) findViewById(R.id.etIp);
ipscan.setOnClickListener(this);
con.setOnClickListener(this);
lv = getListView();
}
class scan extends AsyncTask<Void, Void, ArrayList<ClientScanResult>>{
public Context context;
public scan(Context c) // constructor to take Context
{
context = c; // Initialize your Context variable
}
protected ArrayList<ClientScanResult> doInBackground(Void... params) {
wifiApManager = new WifiApManager(context); // use the variable here
return wifiApManager.getClientList(false);
}
protected void onPostExecute(ArrayList<ClientScanResult> clients){
CustomArrayAdapter cus = new CustomArrayAdapter(connect.this,clients);
lv.setAdapter(cus);
}
}
@Override
public void onClick(View v) {
// TODO Click Action...
if(v == ipscan){
scan myScan = new scan(this); // pass the context to the constructor
myScan.execute();
}
if(v == con){
onConnectButton();
}
}
private void onConnectButton() {
// TODO When Btn s Clicked...
String ip = this.tbIp.getText().toString();
if (ip.matches("^[0-9]{1,4}\\.[0-9]{1,4}\\.[0-9]{1,4}\\.[0-9]{1,4}$")) {
try {
Settings.setIp(ip);
Intent intent = new Intent(connect.this,MouseFragment.class);
connect.this.startActivity(intent);
//Intent i = new Intent(this, MouseFragment.class);
//this.startActivity(i);
this.finish();
} catch (Exception ex) {
Toast.makeText(this, this.getResources().getText(R.string.invalid_ip), Toast.LENGTH_LONG).show(); **//this toast is displayed**
Log.d(TAG, ex.toString());
}
} else {
//this.tvError.setText("Invalid IP address");
//this.tvError.setVisibility(View.VISIBLE);
Toast.makeText(this, this.getResources().getText(R.string.in_valid), Toast.LENGTH_LONG).show();
}
};
/** OS kills process */
public void onDestroy() {
super.onDestroy();
}
/** App starts anything it needs to start */
public void onStart() {
super.onStart();
}
/** App kills anything it started */
public void onStop() {
super.onStop();
}
}
EDIT
01-09 21:23:28.663: W/System.err(5657): java.lang.NullPointerException
01-09 21:23:28.665: W/System.err(5657): at com.arul.remoteit.Settings.setIp(Settings.java:67)
01-09 21:23:28.668: W/System.err(5657): at com.arul.remoteit.connect.onConnectButton(connect.java:81)
01-09 21:23:28.668: W/System.err(5657): at com.arul.remoteit.connect.onClick(connect.java:72)
01-09 21:23:28.669: W/System.err(5657): at android.view.View.performClick(View.java:4445)
01-09 21:23:28.670: W/System.err(5657): at android.view.View$PerformClick.run(View.java:18429)
01-09 21:23:28.672: W/System.err(5657): at android.os.Handler.handleCallback(Handler.java:733)
01-09 21:23:28.673: W/System.err(5657): at android.os.Handler.dispatchMessage(Handler.java:95)
01-09 21:23:28.673: W/System.err(5657): at android.os.Looper.loop(Looper.java:136)
01-09 21:23:28.674: W/System.err(5657): at android.app.ActivityThread.main(ActivityThread.java:5081)
01-09 21:23:28.675: W/System.err(5657): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 21:23:28.677: W/System.err(5657): at java.lang.reflect.Method.invoke(Method.java:515)
01-09 21:23:28.678: W/System.err(5657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
01-09 21:23:28.679: W/System.err(5657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 21:23:28.680: W/System.err(5657): at dalvik.system.NativeStart.main(Native Method)
01-09 21:25:34.395: W/IInputConnectionWrapper(5657): getTextBeforeCursor on inactive InputConnection
01-09 21:25:34.513: W/IInputConnectionWrapper(5657): getTextBeforeCursor on inactive InputConnection
How many time the button is clicked the Exception is Showed with the invalid_ip toast message
Upvotes: 0
Views: 424
Reputation: 36289
First, trouble-shooting Exceptions is called Debugging. Second, there are several way to debug.
For starts, you should find out what line is causing the exception. You can do this by looking at the topmost line of the stack trace. If this is not a reference to code you wrote, check the next line, then the next, etc - until you find something you wrote. If you do not find such a line, skip to step 3.
Once the line is found, add several print statements before the line to print the current variables. For example:
Log.d("DEBUG", "myVariable = " + myVariable == null ? "null" : myVariable);
Then run it and check the output.
The final step is to use the Eclipse Debugger. If you know the line where the code breaks, add a breakpoint by left clicking on the line number. If not, just add the breakpoint on the first line of onCreate
. The link above tells how to use this feature in detail.
Also remember that Android is shipped with many debug tools. You can read about them here.
Upvotes: 1
Reputation: 1410
The key problem is what you're doing in your catch
clause. Simply change the Log.d
to exception.printStackTraceprintStackTrace()
and that will give you more information about the line causing your issue.
Upvotes: 0
Reputation: 42176
Your question, as stated, asks about how to troubleshoot this.
You need to figure out what's null
on the line throwing the Exception
. To do that, you take a look at the stack trace to see which line is causing the problem. Then you either step through your program with a debugger (or a piece of paper and a pencil) or just add print statements to figure out which variable is null
.
When you know which variable is null
, you can trace back through the program to figure out why it's null
. When you know why it's null
, you can fix your problem.
Upvotes: 10