Can't print out String array element!

Posted on 2011-10-06
Medium Priority
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 = bufIn.read();
                // Check for EOF
                if (data == -1) {
                } else {
                    temp = temp + (char) data;
Question by:snajalm

Accepted Solution

evedder earned 1000 total points
ID: 36928972
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

ID: 36929014
Well done!  Thanks heaps!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 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