gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:56:25 26 4
gpt4 key购买 nike

我正在尝试制作一个简单的颜色操作程序,该程序允许您更改窗口中框的颜色。我通过将一些预设颜色从文件中读取到我专门设置来包含这些值的类中来使用它们。我使用数组来包含所有预设值,当我尝试访问该数组的各个元素时,我不断收到空指针异常。这是我第一次尝试使用 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++;
}

关于java - 尝试访问 java 中另一个类中的整数时出现空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13795512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com