Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String array

 
gopala krishna muvvala
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting problem in this code

1 String from[];
2 int count=0;
3 Scanner s=new Scanner(System.in());
4 System.out.println("Enter stings..");
5 from[count]=s.nextLine();
6 count++;
I am geting error in line 5 that is Nullpointer Exception...
Why is ther any wrong in this code......?
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should instantiate the "from" array object.
 
Sam Aran
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gopala krishna muvvala:
I am getting problem in this code

1 String from[];
2 int count=0;
3 Scanner s=new Scanner(System.in());
4 System.out.println("Enter stings..");
5 from[count]=s.nextLine();
6 count++;
I am geting error in line 5 that is Nullpointer Exception...
Why is ther any wrong in this code......?


It's probably better to use an ArrayList. Since you're using Scanner, I'm assuming you're running at least Java 5.

 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Amit said the null pointer exception happens because the reference form does not point to an instantiated String array object.

There is another small thing here to note.

You can compile the following code and run to simulate the error.

But if you use declaration within the method test you will get an compiler error. This is because class level members are initialized automatically (if that's the correct word) and local members need to be explicitly initialized (Objects to null, primitives to certain values).

 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gopala krishna muvvala:
I am getting problem in this code

1 String from[];
2 int count=0;
3 Scanner s=new Scanner(System.in());
4 System.out.println("Enter stings..");
5 from[count]=s.nextLine();
6 count++;
I am geting error in line 5 that is Nullpointer Exception...
Why is ther any wrong in this code......?
 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jamesdeveloper developer:

I think Amit is right here the reason of arising null pointer exception is only because of String Array is not initialized.
[QB][/QB]
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morning, Gopala, and welcome to JavaRanch

Amit is correct that you haven't initialised your array but might have explained a bit more; you need to put

from = new . . .;

somewhere in the code. I presume you know what should go after the "new". I presume you are declaring the array as a field, otherwise you would have a compiler error saying, "from might not have been initialised."
Please find the code button, which makes your code easier to read, and use ctrl-c ctrl-v to quote code; you have introduced a misprint saying System.in() when you mean System.in.

Samus Aran makes a good point that an ArrayList would be easier to use, but you might not have come across it yet. If you do use an ArrayList, however, it is better to declare it as a List, so the first line of code would read

List<String> from = new ArrayList<String>();

You would have to import List and ArrayList, rather like importing Scanner.
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by myself:
"jamesdeveloper developer" please check for an important administrative private message I have just sent you.


Same again, I am afraid.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:


Same again, I am afraid.

"Samus Aran",

Will you please do the same? Although Metroid is a cool video game series, fictional character names are not allowed as display names on this board.
 
sriramaraju vysyaraju
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should initialize the array when it declare even it is an array.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic