File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String array" Watch "String array" New topic
Author

String array

gopala krishna muvvala
Greenhorn

Joined: Aug 26, 2008
Posts: 3
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

You should instantiate the "from" array object.


SCJP, SCWCD.
|Asking Good Questions|
Sam Aran
Greenhorn

Joined: Sep 06, 2008
Posts: 12
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

Joined: Aug 05, 2008
Posts: 347
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

Joined: Sep 07, 2008
Posts: 58
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

Joined: Sep 07, 2008
Posts: 58
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

Joined: Oct 13, 2005
Posts: 38851
    
  23
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

Joined: Oct 13, 2005
Posts: 38851
    
  23
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sriramaraju vysyaraju
Greenhorn

Joined: Sep 09, 2008
Posts: 8
You should initialize the array when it declare even it is an array.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String array