#string help
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
code is
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
The error message you provided suggests that you are trying to access the value field of a null object. This typically occurs when you are trying to perform operations on a variable that has not been initialized.
To help you further, please provide the code you have written so far. Without seeing your code, it is difficult to pinpoint the exact issue and provide an accurate solution.
class stringsort { public static void main(String[]args) { String s = "Josh Rekha Rachna Tanay Vansh Vikrant"; s = s + " "; String word = ""; String arr[] = new String[s.length()]; int z = 0; for(int i=0;i<s.length();i++) { if(s.charAt(i) != ' ') { word = word + s.charAt(i); } else { arr[z] = word; word = ""; z++; } } for(int print1=0;print1<s.length();print1++) { System.out.println(arr[print1]); } for(int y=0;y<s.length();y++) { for(int x=0;x<s.length();x++) { if(((arr[x]).compareTo(arr[x+1])) > 0) { String j = arr[x+1]; arr[x+1] = arr[x]; arr[x] = j; } } } } }
why are you not using String#split?
its not allowed in exams
lmao
this is so dumb
it is
also
uh
in the output
it showed the words seperately just fine
then it kept saying null
there is no anotherString in your piece of code
error is "java.lang.NullPointerException: Cannot read field "value" because "anotherString" is null"
can you pls show us the full erro log instead
alrught
one sec
java.lang.NullPointerException: Cannot read field "value" because "anotherString" is null
at java.base/java.lang.String.compareTo(String.java:2015)
at stringsort.main(stringsort.java:31)
String arr[] = new String[s.length()];
here you are creating your array
s.length() is the length of your String
but you have less words split by whitespace than single characters
and it ends up with nulls at the end
either check if its null before using it or filter out all nulls and store that into a new array
anything works
alr ill try
thanks man, its 2 rn and i got my exam tomorrow
ill let u know if i need any more help?
ok all good
thanks man, real one ☝️
sorted that problem
and addded something so i can sort all those names in alphabetical order
now its showing the same error
for(int y=0;y<z;y++) { for(int x=0;x<z;x++) { if(((arr[x]).compareTo(arr[x+1])) > 0) { String j = arr[x+1]; arr[x+1] = arr[x]; arr[x] = j; } } }
thats my code for sorting
hold on ill send the whole thing
class stringsort
{ public static void main(String[]args) { String s = "Vikrant Rekha Josh Rachna Tanay Vansh"; s = s + " "; String word = ""; String arr[] = new String[s.length()]; int z = 0; for(int i=0;i<s.length();i++) { if(s.charAt(i) != ' ') { word = word + s.charAt(i); } else { arr[z] = word; word = ""; z++; } } for(int print1=0;print1<z;print1++) { System.out.println(arr[print1]); } for(int y=0;y<z;y++) { for(int x=0;x<z;x++) { if(((arr[x]).compareTo(arr[x+1])) > 0) { String j = arr[x+1]; arr[x+1] = arr[x]; arr[x] = j; } } } for(int print=0;print<s.length();print++) { System.out.println(arr[print]); } } }
seems like you didnt check anything
where
yeah but the problem is that either arr[x] is null or arr[x + 1]
so you need to check that before
i mean
like when i split the sentence into words
it did show "josh" first
no null
and the x value here is 0
so arr[0] should be josh
you will hit an value in your array that is null
hm
why do you have two for loops though?:
for(int y=0;y<z;y++)
{
for(int x=0;x<z;x++)
{
if(((arr[x]).compareTo(arr[x+1])) > 0)
{
String j = arr[x+1];
arr[x+1] = arr[x];
arr[x] = j;
}
}
}
outer loop for number of iterations
inner loop for checking
im trying to bubble sort
but for strings
ah ok
I think your algo is wrong though
also the null problems comes from this:
for(int x=0;x<z;x++)
try using this instead:
for(int x=0; x < z - 1; x++)
because you later check arr[x + 1]
yeah
ohh
but your array is actually larger than z
so you dont get an out of bounds
but instead your null value
but then why didnt it print the null like last time
wdym
nvm
youre right
i did the z-1
for x
now theres no error
but its printing null
smth is wrong with the size
because of this:
for(int print=0;print<s.length();print++)
{
System.out.println(arr[print]);
}
you are using s.length for this
instead of your z
also you should fix your formatting
you would write a for loop like this instead:
for(int print=0;print<s.length();print++) {
System.out.println(arr[print]);
}
got a bit of bluej habit
ill try that hold on
yo it worked
alright, rest should be easy
ill be adding more
so all my doubts are clear now
thanks alot bro
nice