返回类型为String的方法,适用于System.out.println(),但如果我想在JLabel中使用创建的String,则不会(A method with return type String that works for System.out.println(), but not if I want to use the created String in a JLabel)

好的,这就是交易...我有一个类型为Student的静态对象,名为workingStudent。 学生有一种方法可以将学生的姓名作为字符串返回。

我有一个带有2张牌的CardLayout JPanel的JFrame。 卡1是登录屏幕,登录成功后,将workingStudent设置为通过此代码与用户帐户关联的任何对象。

private void validateLogin(ArrayList<Student> students){ boolean valid = false; for(int i = 0; i < students.size(); i++){ if(username.getText().equals(students.get(i).getUsername()) && password.getText().equals(students.get(i).getPassword())) { valid = true; setWorkingStudent(students.get(i)); currentSemester=(students.get(i).getLastSemester()); System.out.println("Successful Login!"); cl.show(cards, HOMEPANEL); System.out.println(workingStudent.getName()); } } if(valid == false){ System.out.println("Invalid Login, try again"); } }

这似乎工作得很好。 运行此方法时,它会打印出workingStudents名称而不会出现错误。 这让我相信它也设置了workingStudent也没有任何错误。

问题出现在以下代码中,其中workingStudent.getName()应添加到JLabel的文本中。

private JPanel homePanel() { JPanel home = new JPanel(); home.setLayout(null); home.setBackground(Color.DARK_GRAY); JLabel hi = new JLabel("Hello, "+workingStudent.getName()); hi.setSize(400, 100); hi.setLocation(10,10); hi.setFont(new Font("Serif", Font.BOLD, 36)); hi.setForeground(Color.WHITE); home.add(hi); return home; }

但相反,我得到一个NullPointerException。 我正在寻找一些帮助/任何可能的错误解释。

getName()在validateLogin()中运行得怎么样,但对于homePanel()却没有?

任何帮助将不胜感激。

哦,这是我使用validateLogin的地方(它响应按钮点击)

public void actionPerformed(ActionEvent e) { if (e.getSource()==login){ System.out.println("Logging in..."); validateLogin(students); } else if (e.getSource()==register){ } }

Okay so here's the deal... I have a static object of type Student, named workingStudent. Student has a method to return the student's name as a String.

I have a JFrame with a CardLayout JPanel that has 2 cards. card 1 is a login screen, and upon successful login, sets workingStudent to whatever object was associated with the users account via this code..

private void validateLogin(ArrayList<Student> students){ boolean valid = false; for(int i = 0; i < students.size(); i++){ if(username.getText().equals(students.get(i).getUsername()) && password.getText().equals(students.get(i).getPassword())) { valid = true; setWorkingStudent(students.get(i)); currentSemester=(students.get(i).getLastSemester()); System.out.println("Successful Login!"); cl.show(cards, HOMEPANEL); System.out.println(workingStudent.getName()); } } if(valid == false){ System.out.println("Invalid Login, try again"); } }

This appears to be working just fine. When this method is run, it prints out the workingStudents name with no errors. Which leads me to believe that it also sets workingStudent without any errors as well.

The problem appears in the following code, where workingStudent.getName() should be added to the JLabel's text.

private JPanel homePanel() { JPanel home = new JPanel(); home.setLayout(null); home.setBackground(Color.DARK_GRAY); JLabel hi = new JLabel("Hello, "+workingStudent.getName()); hi.setSize(400, 100); hi.setLocation(10,10); hi.setFont(new Font("Serif", Font.BOLD, 36)); hi.setForeground(Color.WHITE); home.add(hi); return home; }

But instead, I get a NullPointerException. I'm looking for some help/any possible explanation of what could be wrong.

How is it that getName() works just fine in validateLogin() but not for homePanel()??

Any help would be greatly appreciated.

Oh, and this is where I use validateLogin (it responds to a button click)

public void actionPerformed(ActionEvent e) { if (e.getSource()==login){ System.out.println("Logging in..."); validateLogin(students); } else if (e.getSource()==register){ } }

最满意答案

看起来你的程序在创建workingStudent变量之前调用homePanel方法。 尝试以这种方式修改代码:

private JPanel homePanel() { JPanel home = new JPanel(); home.setLayout(null); home.setBackground(Color.DARK_GRAY); String greeting = workingStudent==null?"Hello, unknown user":"Hello, "+workingStudent.getName(); JLabel hi = new JLabel(greeting); hi.setSize(400, 100); hi.setLocation(10,10); hi.setFont(new Font("Serif", Font.BOLD, 36)); hi.setForeground(Color.WHITE); home.add(hi); return home; }

It looks like your program invokes homePanel method before workingStudent variable created. Try modify your code this way:

private JPanel homePanel() { JPanel home = new JPanel(); home.setLayout(null); home.setBackground(Color.DARK_GRAY); String greeting = workingStudent==null?"Hello, unknown user":"Hello, "+workingStudent.getName(); JLabel hi = new JLabel(greeting); hi.setSize(400, 100); hi.setLocation(10,10); hi.setFont(new Font("Serif", Font.BOLD, 36)); hi.setForeground(Color.WHITE); home.add(hi); return home; }

更多推荐