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

15th Mar 2020, 8:36 AM
Souptik Nath
Souptik Nath - avatar
9 ответов
+ 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.
15th Mar 2020, 9:11 AM
A͢J
A͢J - avatar
+ 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++; }
15th Mar 2020, 9:25 AM
A͢J
A͢J - avatar
+ 2
AJ Anant Thanks bro the problem is solved.
15th Mar 2020, 9:46 AM
Souptik Nath
Souptik Nath - avatar
+ 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 " "
15th Mar 2020, 8:44 AM
A͢J
A͢J - avatar
+ 1
AJ Anant The problem is not solved visit the code again(I have made corrections you told me)
15th Mar 2020, 9:01 AM
Souptik Nath
Souptik Nath - avatar
+ 1
SOUPTIK NATH Take all input at the same time in seperate lines.
15th Mar 2020, 9:02 AM
A͢J
A͢J - avatar
+ 1
AJ Anant The input pattern will be 1 string,1int,3 name stored in 1 string with spaces.So total 5.
15th Mar 2020, 9:16 AM
Souptik Nath
Souptik Nath - avatar
+ 1
AJ Anant I have used StringTokenizer to extract each name of the 3 names stored in 1 string
15th Mar 2020, 9:17 AM
Souptik Nath
Souptik Nath - avatar
+ 1
See the question in the code coach(LINK GIVEN IN MY QUESTION)
15th Mar 2020, 9:21 AM
Souptik Nath
Souptik Nath - avatar