[英]error with DataOutputStream
I keep getting an NullPointerException, and I dont know why. 我不断收到NullPointerException,但我不知道为什么。 what the class is supposed to do, is to send whatever I write in my jtextfield as writeUTF, but I keep getting a NullPointerException at
该类应该执行的操作是将我在jtextfield中编写的任何内容发送为writeUTF,但是我一直在获得NullPointerException
dos = new DataOutputStream(socket.getOutputStream());
The GUI code: GUI代码:
public class GUI {
private JFrame frame = new JFrame("Dryck & Ingrediens"); // GUI
private JTextField jtf = new JTextField();// GUI
private JTextArea jl1 = new JTextArea();// GUI
private JList jl = new JList();// GUI
private JScrollPane js = new JScrollPane(jl);// GUI
private DataOutputStream dos;// ServerHandler
private JLabel lab = new JLabel("Ange dryck");//GUI
private JLabel lab1 = new JLabel("Walid Shams");
private JLabel lab2 = new JLabel("Kushtrim Brahimi");
private HashMap<String, ArrayList<String>> drinkar = null;//Controller
private DataInputStream dis;
private Socket socket;
private ServerHandler serverH;
public GUI() {
frame.getContentPane().setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(50, 300, 420, 400);
frame.setResizable(false);
frame.setVisible(true);
js.add(jl);
js.add(jl1);
jl1.setEditable(false);
lab.setBounds(170, 20, 130, 20);
lab1.setBounds(300, 310, 130, 20);
lab2.setBounds(300, 330, 130,20);
jtf.setBounds(130, 40, 150, 40);
jl.setBounds(50, 90, 150, 200);
jl1.setBounds(210, 90, 150, 200);
Container con = frame.getContentPane();
con.setBackground(Color.cyan);
This is where the error accures and where i get the nullpointerexception everytime i type something in my jtextfield. 这是错误发生的地方,而且每次我在jtextfield中键入内容时,我都会得到nullpointerexception。
jtf.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (dos == null) {
if (jtf.getText().length() > 0) {
try {
dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(jtf.getText());
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
String[] empty = new String[]{""};
jl.setListData(empty);
}
}
}
}
);
jl.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (jl.getSelectedIndex() != -1) {
String item = (String) jl.getSelectedValue();
jl1.setText("");
for (String ingrediens : drinkar.get(item)) {
jl1.append(ingrediens + "\n");
}
}else{
jl1.setText("");
}
}
});
frame.add(jtf);
frame.add(jl);
frame.add(jl1);
frame.add(lab);
frame.add(lab1);
frame.add(lab2);
frame.add(js);
}
// tar emot arrayen, lagrar i ny array och visar i JList
public void getArrayList() {
String[] arr = null;
for(int i = 0; i < arr.length; i++){
arr = serverH.setList();
}
jl.setListData(arr);
}
public static void main(String[] args) {
GUI g = new GUI();
}
}
Regarding: 关于:
dos = new DataOutputStream(socket.getOutputStream());
The only variable that can throw an NPE on this line is socket which begs the question: where do you initialize socket? 可以在该行上引发NPE的唯一变量是套接字,它引发了一个问题:您在哪里初始化套接字?
Answer: you don't. 答:你不知道。
Solution: fix this. 解决方案:解决此问题。
Please always check the variables on the line that throws the NPE as that will show you the cause for the error 99% of the time. 请始终检查引发NPE的行上的变量,因为这将在99%的时间内向您显示错误原因。 You haven't appeared to have tried to do this yet.
您似乎尚未尝试执行此操作。
You define an instance variable »serverH« 您定义一个实例变量»serverH«
private ServerHandler serverH;
but don't assign any object to it. 但不要为其分配任何对象。 Therefore you should be facing the NullPointerException.
因此,您应该面对NullPointerException。 You should assign an object to it, eg
您应该为其分配一个对象,例如
serverH = new ServerHandler();
This should solve your problem regarding the NullPointerException. 这应该可以解决有关NullPointerException的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.