Can't print out String array element!

Posted on 2011-10-06
Last Modified: 2012-05-12
How do you usually print a String element in an array,

I use

System.out.println(array[ i ]);

and the attay is defined and initialized as,

String array[] = null;

It's worth mentioning that in a for loop I construct a String "temp" variable which gets filled in like the following!  I can print out temp but I get a NullPointerException when I try to print the array version of this!

for (;;) {
                data =;
                // Check for EOF
                if (data == -1) {
                } else {
                    temp = temp + (char) data;
Question by:snajalm
    LVL 3

    Accepted Solution

    well as far as i know arrays in java can not be dynamic, for that you need to use a Collection, so in your code you need to initialize your array with some elements or you can initialize it as null but then you need to assign another array later.

    I would use an ArrayList instead of an array

    List<Character> array = new ArrayList<Character>();

    array.add( new Character(data))


    Author Closing Comment

    Well done!  Thanks heaps!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    The viewer will learn how to implement Singleton Design Pattern in Java.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now