Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

NullPointerException

  Asked By: Nisha    Date: Aug 01    Category: Java    Views: 447
  

please help me, how can i solve taht nullpointer exception problem?
or i can't do so :

import java.util.*;
import java.io.*;
public class push{
public static void main(String args[])throws IOException{
Stack ma[]=new Stack[5];
int anothe[]={1,22,3,16,1};
String sk[]={"hi","it's","me","not","you"};
int i;

for (i=0; i<6; i++) {
try{
ma[i].add(sk[i]);
} catch(RuntimeException e) {
System.out.print(e+" "+i);
}
}
for (i=0; i<24; i++) System.out.println(ma[i].pop());
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Kay Rodriguez     Answered On: Aug 01

Your first for loop goes from 0 to 5 that is 6 different elements and I
don't think you want 6 elements (both ma[] and sk[] are 5 elements
long). The challenge for you in this question is that you have two
different arrays feeding information.

I would look at using Stack ma = new Stack(); Instead of Stack ma[] =
new Stack[5]; I think that what you are doing currently is creating a
array of Stack Objects.

Now looping though a length of a collection is realy easy, in fact you
have probably seen a example of this in one of your tutes. If you type
arrayName.length or collectionName.getSize() it reports the size of the
array/collection.

for (int i = 0; i <= arrayName.length ; i++) will loop though the exact
size of the array.

Your second for loop is probably going to be broken too.

 
Didn't find what you were looking for? Find more on NullPointerException Or get search suggestion and latest updates.