简体   繁体   中英

Null pointer when trying to access an integer in another class in java

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?

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.

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++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM