0

I have a code. This code splits multi-paragraph text with spaces or quotes. I want put the splitting parts into an array. But when I use that way, I get NullPointException error. What do you think about that?

    arrays = null;

            //arrays [k] = str.substring(j , (i)).trim() ;




    //System.out.println(arrays [k]);

**The arrays and scan are defined above the main. When i try the ArrayList, i get output but it is multiple. For example: a b c d a b c d . . Multi-lines of same thing.

5
  • the variable arrays isn't initialized anywhere, after its declaration. Unless it is required that you use arrays, you might use an ArrayList, and when you exit the reading loop you can convert it to an array. Commented Mar 29, 2014 at 19:21
  • ArrayList is giving me wrong output Commented Mar 29, 2014 at 19:28
  • The wrong output has to do with your control flow (the reading loop and the conditionals driving the text processing) and not with you using lists over arrays. Commented Mar 29, 2014 at 19:48
  • Okay, so what will I do about that? Commented Mar 29, 2014 at 19:52
  • You'll have to rethink your algorithm, or debug it to pinpoint what goes wrong and where in the code. I'd use a pre-typed text read in one shot from file, then I would apply the splitting algorithm to it and debug step by step until I figure out what's wrong. Commented Mar 29, 2014 at 20:05

3 Answers 3

1

You declared your arrays = null;, so when you try to access arrays, you are accessing null in fact, so you get a NullPointerException. You want to declare your arrays as String[] arrays = new String[x] with x being the size of the array. If you don't know the size in advance, you would want to use an ArrayList. You declare that by saying List<String> list = new ArrayList<String>() (from Java 5 up for the generics). You add elements by saying list.add(item);

Sign up to request clarification or add additional context in comments.

Comments

0

what i can see by just a quick look is you have declared String[] arrays = null; but not initializing it and then using it arrays [k], this is a null pointer. you have to initialize it. Something like String[] arrays = new String[6];. You might want to add the length dynamically.

5 Comments

Thanks, but if the file's size is bigger than my entered size?
did not got your question. Please explain what do you mean by "than my entered size" ?
OP means "if the size is not known in advance, I cannot initialize an array of unknown size"
For example = new String [26].. If my splitting parts are major than, bigger than 26. (Just example.)
Ok. No worries, you can create a starting size array lets say 10 and then you can write logic to grow it each time you need more. Or just use Collection List
0

just try using "try catch"

compare using "compareTo" method

char c1 = 'x'; 
char c2 = 'y'; 
c1.compareTo(c2) 

and dont initialize array as null

4 Comments

compare using "compareTo" method char c1 = 'x'; char c2 = 'y'; c1.compareTo(c2) and dont initialize array as null
If I don't initialize as null, the compiler gives me error
String[] arrays = new String[1024]
If the splitting parts bigger than 1024, i don't know how many lines in text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.