0
Can anyone tell what is wrong in my code?
It is showing java nullpointer exception.It is a code coach program(Question:https://www.sololearn.com/coach/18?ref=app) My program:https://code.sololearn.com/cYljeCQyvdRD/?ref=app
9 Answers
+ 2
SOUPTIK NATH You defined size of array 5 but your array containing 2 or 3 values but loop will iterate till 5 times and after 2 or 3 times you will get null value which you have to handle.
So I will suggest use ArrayList instead of Array. Because ArrayList creates dynamic size.
+ 2
SOUPTIK NATH See this example.
Eric
2
AJ Anant SOUPTIK
Here you are storing 3rd String in an array but you have defined size of Array 5. When you print the length of this array after storing value you will get length 5. Actually which is not. It contains length 3.
Just print a.length after storing value in array and see what size you are getting.
Just put this section inside try catch block
if(a[i].charAt(0)==j)
{
t=a[k];
a[k]=a[i];
a[i]=t;
if(a[i]==name)
myk=k;
k++;
}
+ 2
AJ Anant Thanks bro the problem is solved.
+ 1
Take input like this.
String s1 = sc.next();
int n = sc.nextInt();
String s2 = "";
if(sc.nextLine() != "")
s2 = sc.nextLine();
convert s2 in aray after splitting with " "
+ 1
AJ Anant The problem is not solved visit the code again(I have made corrections you told me)
+ 1
SOUPTIK NATH Take all input at the same time in seperate lines.
+ 1
AJ Anant The input pattern will be
1 string,1int,3 name stored in 1 string with spaces.So total 5.
+ 1
AJ Anant I have used StringTokenizer to extract each name of the 3 names stored in 1 string
+ 1
See the question in the code coach(LINK GIVEN IN MY QUESTION)