Earrold Valdez
Earrold Valdez

Reputation: 3

where to set nullpointerexception

im very new in developing an android app.. when i run my program in the emulator it force close.. i figure it out that i need to set an nullpointerexception but i dont know where to set those stuff.. pls help me.

public class SampleActivity extends Activity {

TextView dp_octet1, dp_octet2, dp_octet3, dp_octet4, dp_slashes;
int i = 0;
int j = 0;
int octet[];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);
    iniTextView();
    generatingRandomIpAddress();
}

private void generatingRandomIpAddress() {


   Random i = new Random();           //Random number between 0-2
   if(i.nextInt(3)==0){
       octet[0] = 8+(i.nextInt(22));
       octet[1] = 1+(i.nextInt(126));
   }
   if(i.nextInt(3)==1){
       octet[0] = 16+(i.nextInt(14));
       octet[1] = 128+(i.nextInt(63));
   }
   if(i.nextInt(3)==2){
       octet[0] = 24+(i.nextInt(6));
       octet[1] = 192+(i.nextInt(31));
   }
   octet[2] = (i.nextInt(255));
   octet[3] = (i.nextInt(255));
   octet[4] = (i.nextInt(255));

   //Display the problem
   dp_slashes.setText(octet[0]);
   dp_octet1.setText(octet[1]);
   dp_octet2.setText(octet[2]);
   dp_octet3.setText(octet[3]);
   dp_octet4.setText(octet[4]);

}



private void iniTextView() {
    // TODO Auto-generated method stub
    dp_octet1 = (TextView) findViewById(R.id.octet1);
    dp_octet2 = (TextView) findViewById(R.id.octet2);
    dp_octet3 = (TextView) findViewById(R.id.octet3);
    dp_octet4 = (TextView) findViewById(R.id.octet4);
    dp_slashes = (TextView) findViewById(R.id.slashes);
}

log cat

08-05 09:11:07.180: D/AndroidRuntime(1286): Shutting down VM 08-05 09:11:07.180: W/dalvikvm(1286): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 08-05 09:11:07.203: E/AndroidRuntime(1286): FATAL EXCEPTION: main 08-05 09:11:07.203: E/AndroidRuntime(1286): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myscratches/com.example.myscratches.SampleActivity}: java.lang.ArrayIndexOutOfBoundsException 08-05 09:11:07.203: E/AndroidRuntime(1286): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 08-05 09:11:07.203: E/AndroidRuntime(1286): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 08-05 09:11:07.203: E/AndroidRuntime(1286): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 08-05 09:11:07.203: E/AndroidRuntime(1286): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 08-05 09:11:07.203: E/AndroidRuntime(1286): at android.os.Handler.dispatchMessage(Handler.java:99) 08-05 09:11:07.203: E/AndroidRuntime(1286): at android.os.Looper.loop(Looper.java:123) 08-05 09:11:07.203: E/AndroidRuntime(1286): at android.app.ActivityThread.main(ActivityThread.java:4627) 08-05 09:11:07.203: E/AndroidRuntime(1286): at java.lang.reflect.Method.invokeNative(Native Method) 08-05 09:11:07.203: E/AndroidRuntime(1286): at java.lang.reflect.Method.invoke(Method.java:521) 08-05 09:11:07.203: E/AndroidRuntime(1286): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 08-05 09:11:07.203: E/AndroidRuntime(1286): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-05 09:11:07.203: E/AndroidRuntime(1286): at dalvik.system.NativeStart.main(Native Method) 08-05 09:11:07.203: E/AndroidRuntime(1286): Caused by: java.lang.ArrayIndexOutOfBoundsException 08-05 09:11:07.203: E/AndroidRuntime(1286): at com.example.myscratches.SampleActivity.generatingRandomIpAddress(SampleActivity.java:32) 08-05 09:11:07.203: E/AndroidRuntime(1286): at com.example.myscratches.SampleActivity.onCreate(SampleActivity.java:24) 08-05 09:11:07.203: E/AndroidRuntime(1286): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-05 09:11:07.203: E/AndroidRuntime(1286): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 08-05 09:11:07.203: E/AndroidRuntime(1286): ... 11 more

Upvotes: 0

Views: 84

Answers (2)

Ashutosh Jindal
Ashutosh Jindal

Reputation: 18869

I am not sure whether this applies to ADT, but if I had a piece of Java code in which a NullPointerException was being thrown and I had to find out where it was being thrown, I would do one of the following :

  1. Go through the stack trace
  2. Put an Exception Breakpoint.

For the #2 above, take a look at this. If you choose 'Uncaught' while setting up this breakpoint, the execution should automatically halt when any NullPointer exception is thrown.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234807

You need to initialize octet before you can start assigning values to its elements. Change your declaration to:

int octet[] = new int[4];

Alternatively, add this line:

octet = new int[4];

to generatingRandomIpAddress() before you start assigning to the elements of octet.

P.S. In the future, when you are asking for help with an exception, you should post the logcat output showing the stack trace for the exception.

Upvotes: 1

Related Questions