gpt4 book ai didi

java - JComboBox 空指针异常。需要从框中获取 Int 并将 int 设置为从框中选择的 int

转载 作者:行者123 更新时间:2023-11-29 05:14:31 25 4
gpt4 key购买 nike

我的程序现在需要加载两个文本文件,我正在按字符串对它们进行排序。 JcomboxBox 应该允许您在 1 和 4 之间选择 int(要比较的字符串的大小)所以 2 会返回“我是”,而 1 会返回“我” 我得到空点异常,我以前从未使用过组合框。请帮忙/

import java.awt.GridLayout;

import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
import javax.swing.JComboBox;


public class Lab8 extends JPanel
{
private JPanel text;
private JComboBox input;
private JLabel label;
private JButton load, go,go2;
private CountList<SuperString> words;
private String filename;
private int width = 400;
private int height = 600;
private TextArea textarea,textarea2;
Scanner scan;

public Lab8()
{
Integer [] select = {1,2,3,4};
JComboBox input = new JComboBox(select);
text = new JPanel(new GridLayout(1,2));
go = new JButton("Select Text File");
go2 = new JButton("Select 2nd Text File");
label = new JLabel("How many sequenced words would you like to analyze? (Must be => 1)" );
input.setSelectedIndex(0);

ButtonListener listener = new ButtonListener();
go.addActionListener(listener);
go2.addActionListener(listener);
input.addActionListener(listener);

textarea = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
textarea2 = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
textarea.setFont(new Font("Helvetica",Font.PLAIN,24));
textarea2.setFont(new Font("Helvetica",Font.PLAIN,24));
textarea.setPreferredSize(new Dimension(width,height));
textarea2.setPreferredSize(new Dimension(width,height));
setPreferredSize(new Dimension(900,600));
text.add(textarea);
text.add(textarea2);
add(input);
add(go);
add(go2);
add(text);

textarea.setText("No File Selected");
textarea2.setText("No File Selected");


}
public class ButtonListener implements ActionListener //makes buttons do things
{
JFileChooser chooser = new JFileChooser("../Text");


public void actionPerformed(ActionEvent event)
{
Integer N = input.getSelectedIndex();

if(event.getSource() == go)
{
int returnvalue = chooser.showOpenDialog(null);

if(returnvalue == JFileChooser.APPROVE_OPTION)
{
try
{
File file = chooser.getSelectedFile();
filename = file.getName();
System.err.println(filename);
scan = new Scanner(file);
}
catch (IOException e)
{
System.err.println("IO EXCEPTION");
return;
}
}
else
{
return;
}

String[] storage = new String[N];
words = new CountLinkedList<SuperString>();
for(int i=1;i<N;i++)
storage[i] = scan.next().toLowerCase().replace(",","").replace(".","");

while(scan.hasNext())
{
for(int i=0;i<=N-2;i++)
storage[i] = storage[i+1];
storage[N-1] = scan.next().toLowerCase();
storage[N-1] = storage[N-1].replace(",","").replace(".","");
SuperString ss = new SuperString(storage);
// System.out.println(ss);
words.add(ss );
}
scan.close();
textarea.append(" "+filename+" has wordcount: "+words.size()+
"\n-------------------------\n\n");

SuperString[] ss = new SuperString[words.size()];
int i=0;
for(SuperString word: words)
{
ss[i] = word;
i++;
}
Arrays.sort(ss, new SuperStringCountOrder());
for(SuperString word : ss)
{
textarea.append(" "+word+"\n");
}
}


if(event.getSource() == go2)
{
int returnvalue = chooser.showOpenDialog(null);

if(returnvalue == JFileChooser.APPROVE_OPTION)
{
try
{
File file = chooser.getSelectedFile();
filename = file.getName();
System.err.println(filename);
scan = new Scanner(file);
}
catch (IOException e)
{
System.err.println("IO EXCEPTION");
return;
}
}
else
{
return;
}
String[] storage = new String[N];
words = new CountLinkedList<SuperString>();
for(int i=1;i<N;i++)
storage[i] = scan.next().toLowerCase().replace(",","").replace(".","");

while(scan.hasNext())
{
for(int i=0;i<=N-2;i++)
storage[i] = storage[i+1];
storage[N-1] = scan.next().toLowerCase();
storage[N-1] = storage[N-1].replace(",","").replace(".","");
SuperString ss = new SuperString(storage);
words.add(ss );
}
scan.close();
textarea2.append(" "+filename+" has wordcount: "+words.size()+
"\n-------------------------\n\n");

SuperString[] ss = new SuperString[words.size()];
int i=0;
for(SuperString word: words)
{
ss[i] = word;
i++;
}
Arrays.sort(ss, new SuperStringCountOrder());
for(SuperString word : ss)
{
textarea2.append(" "+word+"\n");
}
}

}
}


public static void main(String[] arg)
{
JFrame frame = new JFrame("Lab 8");
frame.getContentPane().add(new Lab8());
frame.pack();
frame.setVisible(true);
}
}

最佳答案

您要在构造函数中重新声明 JComboBox 变量,从而隐藏在类中找到的字段。通过这样做,该字段保持为空:

public class Lab8 extends JPanel
{
private JPanel text;
private JComboBox input; // this guy remains null

// .... etc ....

public Lab8()
{
Integer [] select = {1,2,3,4};

// the line below initializes a local input variable.
// this variable is visible only inside of the constructor
JComboBox input = new JComboBox(select); // ***** here ****

不要重新声明变量:

public class Lab8 extends JPanel
{
private JPanel text;
private JComboBox input;

// .... etc ....

public Lab8()
{
Integer [] select = {1,2,3,4};
// JComboBox input = new JComboBox(select); // ***** here ****
input = new JComboBox(select); // ***** note difference? *****

关于java - JComboBox 空指针异常。需要从框中获取 Int 并将 int 设置为从框中选择的 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27048175/

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