0

I'm trying to make a simple color manipulation program that allows you to change the color of a box in a window. I'm using some preset colors by reading them in from a file into a class that I have specifically set up to contain those values. I'm using arrays to contain all the preset values and when I try to access the individual elements of that array, I keep getting a nullpointer exception. This is my first time try to use java, so I assume i'm making a boneheaded mistake. Here is my code:

package color.sampler;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;


public class ColorSampler extends JFrame
{
protected ColorFrame sampler;
public JList colorList;
protected colors [] listOfColors;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException 
{
    new ColorSampler("ColorSampler");
}
public ColorSampler(String title) throws IOException
{
    super(title);
    setBounds(100,100,300,300);
    addWindowListener(new WindowDestroyer());


    sampler = new ColorFrame();

    getContentPane().setLayout(null);

    getContentPane().add(sampler);

    sampler.setBounds(10,10,270,200);
    FileInputStream stream = new FileInputStream("C:\\java input\\input.txt");
    InputStreamReader reader;
    reader = new InputStreamReader(stream);
    StreamTokenizer tokens = new StreamTokenizer(reader);
    int numColors, counter;
    numColors = 11;
    counter = 0;
    listOfColors = new colors[numColors];
    while(tokens.nextToken() != tokens.TT_EOF)
    {
        listOfColors[counter].name = (String)tokens.sval; 
        tokens.nextToken();
        listOfColors[counter].r = (int)tokens.nval;
        System.out.println(listOfColors[counter].r);
        tokens.nextToken();
        listOfColors[counter].g = (int)tokens.nval;
        tokens.nextToken();
        listOfColors[counter].b = (int)tokens.nval;
        counter++;
    }
    stream.close();
    colorList = new JList();
    colorList.addListSelectionListener(new ListHandler());
    String colorString[];
    colorString = new String[numColors];
    for(counter = 0; counter < numColors; counter++)
    {
        colorString[counter] = listOfColors[counter].name;
    }
    colorList.setListData(colorString);
    getContentPane().add(colorList);
    setVisible(true);
    // TODO code application logic here
}
private class ListHandler implements ListSelectionListener
{

    @Override
    public void valueChanged(ListSelectionEvent e) 
    {
        if(e.getSource() == colorList)
        {
            if(!e.getValueIsAdjusting())
            {
                int i = colorList.getSelectedIndex();
                String s = (String) colorList.getSelectedValue();
                System.out.println("Position " + i + " selected: " + s);
            }
        }
    }

}
}

and the class I'm using to store the values:

public class colors 
{
public int r, g, b;
public String name;
public colors()
{
    r = 0;
    g = 0;
    b = 0;
    name = "bob";
}

}

So, how would I fix the issue caused when I try to access the first element's name in the array?

3
  • 3
    in which line you are getting NPE? Commented Dec 10, 2012 at 5:33
  • can you plz post the error log , console out put Commented Dec 10, 2012 at 5:35
  • at which statement you are getting exception. Please write in comments above it Commented Dec 10, 2012 at 5:35

2 Answers 2

2

I guess you need to initialize each object of the listOfColors array also, change your while loop to...

counter = 0;
listOfColors = new colors[numColors];
while(tokens.nextToken() != tokens.TT_EOF)
{
    listOfColors[counter] = new Colors();
    listOfColors[counter].name = (String)tokens.sval; 
    tokens.nextToken();
    listOfColors[counter].r = (int)tokens.nval;
    System.out.println(listOfColors[counter].r);
    tokens.nextToken();
    listOfColors[counter].g = (int)tokens.nval;
    tokens.nextToken();
    listOfColors[counter].b = (int)tokens.nval;
    counter++;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Just because you do this:

listOfColors = new colors[numColors];

doesn't mean that the array has anything in it. In fact, at this point, it is an array of null values. You need to construct a colors object for each element before setting the names and color values.

And by the way, the class name for colors should start with a capital: Colors.

1 Comment

Thanks for the advice on style and the explanation for why my problem was occurring. I changed the class name for colors.

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.