尝试在 java 中访问另一个类中的整数时的空指针



我正在尝试制作一个简单的颜色处理程序,允许您更改窗口中框的颜色。我使用了一些预设颜色,将它们从文件中读入到我专门为包含这些值而设置的类中。我正在使用数组来包含所有预设值,当我尝试访问该数组的各个元素时,我不断收到空指针异常。这是我第一次尝试使用Java,所以我认为我犯了一个愚蠢的错误。这是我的代码:

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

以及我用来存储值的类:

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

那么,我将如何解决尝试访问数组中第一个元素名称时引起的问题?

我想您还需要初始化 listOfColors 数组的每个对象,将 while 循环更改为...

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

只是因为你这样做:

listOfColors = new colors[numColors];

并不意味着数组中有任何内容。 实际上,在这一点上,它是一个空值数组。 在设置名称和颜色值之前,您需要为每个元素构造一个颜色对象。

顺便说一下,颜色的类名应该以大写字母开头:颜色。

最新更新