#string help

1 messages · Page 1 of 1 (latest)

solid fable
#

Im trying to make a class where i seperate string into words, then sort them alphabetically, but it doesnt seem to work

error is "java.lang.NullPointerException: Cannot read field "value" because "anotherString" is null"

silent bobcatBOT
#

<@&987246399047479336> please have a look, thanks.

solid fable
#

code is

silent bobcatBOT
#

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.

solid fable
#

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; } } } } }

short gulch
#

why are you not using String#split?

solid fable
#

its not allowed in exams

short gulch
#

lmao

solid fable
#

yeah

#

pretty dumb rule

short gulch
#

this is so dumb

solid fable
#

it is

#

also

#

uh

#

in the output

#

it showed the words seperately just fine

#

then it kept saying null

short gulch
#

there is no anotherString in your piece of code

solid fable
#

whats an anotherString

#

another string?

short gulch
#

error is "java.lang.NullPointerException: Cannot read field "value" because "anotherString" is null"

solid fable
#

yeah

#

i couldnt figure out what that meant

short gulch
#

can you pls show us the full erro log instead

solid fable
#

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)

short gulch
#

oh your array is way to big

#

so it fills up with nulls at the end

solid fable
#

oh

#

wait

#

so is the length

#

like all the letters

#

and not the words

short gulch
#

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

solid fable
#

oh

#

i see

short gulch
#

and it ends up with nulls at the end

solid fable
#

i see

#

so what can i do to avoid this

short gulch
#

either check if its null before using it or filter out all nulls and store that into a new array

#

anything works

solid fable
#

alr ill try

#

thanks man, its 2 rn and i got my exam tomorrow

#

ill let u know if i need any more help?

short gulch
#

ok all good

solid fable
#

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]); } } }

short gulch
#

seems like you didnt check anything

solid fable
#

i did

#

i think i did

short gulch
#

where

solid fable
#

the line where i used .compareTo

#

if(((arr[x]).compareTo(arr[x+1])) > 0)

short gulch
#

yeah but the problem is that either arr[x] is null or arr[x + 1]

#

so you need to check that before

solid fable
#

i mean

#

like when i split the sentence into words

#

it did show "josh" first

#

no null

#

and the x value here is 0

short gulch
#

yeah that is working fine

#

but if you continue

solid fable
#

so arr[0] should be josh

short gulch
#

you will hit an value in your array that is null

solid fable
#

hm

short gulch
#

wait let me check again

#

I see now that you use z for the counter of your words

solid fable
#

yes

#

because thats the size**

#

so it should be okay

short gulch
#

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;
                }
            }
        }
solid fable
#

outer loop for number of iterations

#

inner loop for checking

#

im trying to bubble sort

#

but for strings

short gulch
#

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]

solid fable
#

OH

#

so like

#

in the final iteration

#

arr[x+1] is out of the arrray size

#

?

short gulch
#

yeah

solid fable
#

ohh

short gulch
#

but your array is actually larger than z

#

so you dont get an out of bounds

#

but instead your null value

solid fable
#

but then why didnt it print the null like last time

short gulch
#

wdym

solid fable
#

nvm

#

youre right

#

i did the z-1

#

for x

#

now theres no error

#

but its printing null

#

smth is wrong with the size

short gulch
#

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]);
}
solid fable
#

yo it worked

#

alright, rest should be easy

#

ill be adding more

#

so all my doubts are clear now

#

thanks alot bro

short gulch
#

nice