Easy Program在多个Action监听器时抛出NullPointerException错误。

huangapple go评论75阅读模式
英文:

Easy Program throws NullPointerException when multiple Action Listeners

问题

我已尝试在3个单独的文件中使用多个动作监听器,但当我想要显示第三个文件时,它会抛出空指针异常,但会抛出此错误。我只是一个初学者,也许我在连接类到另一个类时失败了。这可能是主要问题。提前感谢您的帮助。

源代码:

Start.java

package com.raising.darkness;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Start {

    // 省略了一些代码...

    public class StartHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ex) {
            heroClasses.createClassChoose();
        }
    }
}

HeroClasses.java

```java
package com.raising.darkness;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HeroClasses {
    // 省略了一些代码...

    public class ClassHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent ex) {
            choosenClass = ex.getActionCommand();
            averagePanel.mainPanel();
        }
    }
}

AveragePanel.java

```java
package com.raising.darkness;

import javax.swing.*;
import java.awt.*;

public class AveragePanel {

    // 省略了一些代码...
}

请注意,我已经省略了一些源代码中的部分,只包含了与问题相关的部分。希望这有助于您理解问题的所在。

英文:

I have tried using multiple action listeners in 3 seperate files but it throws NullPointer exception when I want to display a "3-rd file" but it throws this error.I am just a begginer and maybe I failed with connecting class to another.That may be the main problem.Thanks for helping in advance.

Source :

Start.java

    package com.raising.darkness;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Start {
    
        JFrame frame;
        Container container;
        JPanel startTitle;
        JLabel startText;
        Font startTitleFont= new Font("Dialog", Font.BOLD, 60);
        Font startTitleButton = new Font("Dialog", Font.PLAIN, 40);
        Font chooseTitleFont = new Font("Dialog", Font.PLAIN, 59);
        JPanel startButtonPanel;
        JButton startButton;
    
    
        Color backgroundFrame = new Color(180,180,180);
        Color buttonColor = new Color(144,144,144);
    
    
    
        StartHandler startHandler = new StartHandler();
    
    
        AveragePanel averagePanel = new AveragePanel(this);
        HeroClasses heroClasses = new HeroClasses(this);
    
    
        public static void main(String[] args) { new Start(); }
        public Start(){
            frame=new JFrame();
            frame.setSize(1024,800);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Raising Darkness");
            frame.getContentPane().setBackground(backgroundFrame);
            frame.setLayout(null);
    
            container=frame.getContentPane();
    
             //Title Text
            startTitle = new JPanel();
            startTitle.setBounds(162,100,700,150);
            startTitle.setBackground(backgroundFrame);
    
            startText = new JLabel("RAISING DARKNESS");
    
            startText.setFont(startTitleFont);
    
            startTitle.add(startText);
            container.add(startTitle);
    
    
            startButtonPanel = new JPanel();
            startButtonPanel.setBounds(387,600,200,70);
            startButtonPanel.setBackground(backgroundFrame);
            startButton = new JButton();
            startButton.setText("START");
            startButton.setBackground(buttonColor);
    
            startButton.setFont(startTitleButton);
            startButton.setFocusPainted(false);
            startButton.addActionListener(startHandler);
    
            startButtonPanel.add(startButton);
            container.add(startButtonPanel);
    
    
            frame.setVisible(true);
    
        }
    
    
        public class StartHandler implements ActionListener{
    
            @Override
            public void actionPerformed(ActionEvent ex) {
    
                heroClasses.createClassChoose();
            }
        }
    
    
    }

HeroClasses.java

    package com.raising.darkness;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class HeroClasses{
        Start start;
        Color normalChooseColor = new Color(144,144,144);
        JPanel chooseTitlePanel, chooseButtonsPanel;
        JLabel chooseTitleLabel;
        JButton chooseMageButton, chooseWarriorButton, chooseArcherButton;
        String choosenClass;
    
        Color chooseClassColor = new Color(100,100,100);
    
        ClassHandler classHandler = new ClassHandler();
    
        AveragePanel averagePanel = new AveragePanel(this);
    
    
        public HeroClasses(Start start){
                this.start = start;
            }
    
        public void createClassChoose(){
            start.startButtonPanel.setVisible(false);
            start.startTitle.setVisible(false);
            start.frame.getContentPane().setBackground(normalChooseColor);
    
    
            chooseTitlePanel = new JPanel();
            chooseTitlePanel.setBounds(162,200,700,100);
            chooseTitlePanel.setBackground(normalChooseColor);
    
            chooseTitleLabel= new JLabel();
            chooseTitleLabel.setText("Choose Class!");
            chooseTitleLabel.setFont(start.chooseTitleFont);
            chooseTitlePanel.add(chooseTitleLabel);
            start.container.add(chooseTitlePanel);
    
            chooseButtonsPanel = new JPanel();
            chooseButtonsPanel.setBounds(162,500,700,100);
            chooseButtonsPanel.setLayout(new GridLayout(1,3,50,0));
            chooseButtonsPanel.setBackground(normalChooseColor);
    
            //CLASSES
    
            chooseMageButton = new JButton("Mage");
            chooseMageButton.setBackground(chooseClassColor);
            chooseMageButton.setFont(start.startTitleButton);
            chooseMageButton.addActionListener(classHandler);
            chooseMageButton.setActionCommand("Mage");
            chooseMageButton.setFocusPainted(false);
            chooseButtonsPanel.add(chooseMageButton);
    
            chooseWarriorButton = new JButton("Warrior");
            chooseWarriorButton.setBackground(chooseClassColor);
            chooseWarriorButton.setFont(start.startTitleButton);
            chooseWarriorButton.addActionListener(classHandler);
            chooseWarriorButton.setActionCommand("Warrior");
            chooseWarriorButton.setFocusPainted(false);
            chooseButtonsPanel.add(chooseWarriorButton);
    
            chooseArcherButton = new JButton("Archer");
            chooseArcherButton.setBackground(chooseClassColor);
            chooseArcherButton.setFont(start.startTitleButton);
            chooseArcherButton.addActionListener(classHandler);
            chooseArcherButton.setActionCommand("Archer");
            chooseArcherButton.setFocusPainted(false);
            chooseButtonsPanel.add(chooseArcherButton);
    
    
            start.container.add(chooseButtonsPanel);
    
    
    
    
        }
    
    
    
    public class ClassHandler implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent ex) {
            choosenClass = ex.getActionCommand();
            averagePanel.mainPanel();
    
    
        }
    }
    
    }

AveragePanel.java

    package com.raising.darkness;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class AveragePanel {
    
        Start start;
        HeroClasses heroClasses;
        String actualClass;
        JPanel picturePanel, statsPanel, storyPanel, choicesPanel;
        JLabel pictureLabel;
        ImageIcon imageIcon;
    
    
        public AveragePanel(HeroClasses heroClasses){this.heroClasses = heroClasses;}
        public AveragePanel(Start start){this.start = start;}
    
    
    
    
        public void mainPanel(){
            heroClasses.chooseButtonsPanel.setVisible(false);
            heroClasses.chooseTitlePanel.setVisible(false);
    
             actualClass = heroClasses.choosenClass ;
            System.out.println("Current Class is :" +actualClass);
    
            picturePanel  = new JPanel();
            picturePanel.setBounds(50,50,500,350);
            picturePanel.setBackground(Color.WHITE);
                start.container.add(picturePanel);
    
    
    
        }
    
    
    }

答案1

得分: 0

以下是您要翻译的内容:

  1. AveragePanel类中有2个构造函数。您使用了public AveragePanel(HeroClasses heroClasses){this.heroClasses = heroClasses;},所以您的字段start为空,您有两个选择。
    1. 更改mainPanel方法,并从heroClasses获取start面板:heroClasses.start.container.add(picturePanel); 方法将如下所示:
    public void mainPanel(){
        heroClasses.chooseButtonsPanel.setVisible(false);
        heroClasses.chooseTitlePanel.setVisible(false);
    
        actualClass = heroClasses.choosenClass;
        System.out.println("Current Class is :" + actualClass);
    
        picturePanel = new JPanel();
        picturePanel.setBounds(50, 50, 500, 350);
        picturePanel.setBackground(Color.WHITE);
        heroClasses.start.container.add(picturePanel);
    }
    
    1. 更好的解决方案是更改构造函数,并使用heroClasses设置start面板。然后您可以同时使用两个构造函数,方法mainPanel()将按相同的方式工作。以下是构造函数:
    public AveragePanel(HeroClasses heroClasses){
        this.heroClasses = heroClasses;
        this.start = heroClasses.start;
    }
    public AveragePanel(Start start){this.start = start;}
    

    方法mainPanel()没有更改。

最佳实践是在构造函数中初始化字段。在您的情况下,您需要在HeroClasses类中进行更改。通过构造函数初始化averagePanel字段:

public class HeroClasses{
    // 其他字段...
    AveragePanel averagePanel;

    public HeroClasses(Start start){
        this.start = start;
        this.averagePanel = new AveragePanel(this);
    }

    // 其他方法...
}

这是您的翻译,请使用这些翻译来替代原始文本中的相应部分。

英文:

You have 2 constructors in AveragePanel class. You use public AveragePanel(HeroClasses heroClasses){this.heroClasses = heroClasses;}, so your field start is null, you have two options.

  1. Change method main panel, and get start panel from heroClasses: heroClasses.start.container.add(picturePanel); Method will be looks like:

         public void mainPanel(){
         heroClasses.chooseButtonsPanel.setVisible(false);
         heroClasses.chooseTitlePanel.setVisible(false);
    
          actualClass = heroClasses.choosenClass ;
         System.out.println("Current Class is :" +actualClass);
    
         picturePanel  = new JPanel();
         picturePanel.setBounds(50,50,500,350);
         picturePanel.setBackground(Color.WHITE);
         heroClasses.start.container.add(picturePanel);
    
     }
    
  2. Better solution is change constructor, and set start panel using heroClasses. Then you can use both constructors and method mainPanel() will works this same.
    Below constructors:

         public AveragePanel(HeroClasses heroClasses){
     	    this.heroClasses = heroClasses;
     	    this.start = heroClasses.start;
     	}
         public AveragePanel(Start start){this.start = start;}
    

method mainPanel() with no changes.

The best practice is initializion field in constructor. In your case, you have to change that in your HeroClasses class. Initialize field averagePanel by contructor:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HeroClasses{
    Start start;
    Color normalChooseColor = new Color(144,144,144);
    JPanel chooseTitlePanel, chooseButtonsPanel;
    JLabel chooseTitleLabel;
    JButton chooseMageButton, chooseWarriorButton, chooseArcherButton;
    String choosenClass;

    Color chooseClassColor = new Color(100,100,100);

    ClassHandler classHandler = new ClassHandler();

    AveragePanel averagePanel;


    public HeroClasses(Start start){
            this.start = start;
            this.averagePanel = new AveragePanel(this);
        }

    public void createClassChoose(){
        start.startButtonPanel.setVisible(false);
        start.startTitle.setVisible(false);
        start.frame.getContentPane().setBackground(normalChooseColor);


        chooseTitlePanel = new JPanel();
        chooseTitlePanel.setBounds(162,200,700,100);
        chooseTitlePanel.setBackground(normalChooseColor);

        chooseTitleLabel= new JLabel();
        chooseTitleLabel.setText("Choose Class!");
        chooseTitleLabel.setFont(start.chooseTitleFont);
        chooseTitlePanel.add(chooseTitleLabel);
        start.container.add(chooseTitlePanel);

        chooseButtonsPanel = new JPanel();
        chooseButtonsPanel.setBounds(162,500,700,100);
        chooseButtonsPanel.setLayout(new GridLayout(1,3,50,0));
        chooseButtonsPanel.setBackground(normalChooseColor);

        //CLASSES

        chooseMageButton = new JButton("Mage");
        chooseMageButton.setBackground(chooseClassColor);
        chooseMageButton.setFont(start.startTitleButton);
        chooseMageButton.addActionListener(classHandler);
        chooseMageButton.setActionCommand("Mage");
        chooseMageButton.setFocusPainted(false);
        chooseButtonsPanel.add(chooseMageButton);

        chooseWarriorButton = new JButton("Warrior");
        chooseWarriorButton.setBackground(chooseClassColor);
        chooseWarriorButton.setFont(start.startTitleButton);
        chooseWarriorButton.addActionListener(classHandler);
        chooseWarriorButton.setActionCommand("Warrior");
        chooseWarriorButton.setFocusPainted(false);
        chooseButtonsPanel.add(chooseWarriorButton);

        chooseArcherButton = new JButton("Archer");
        chooseArcherButton.setBackground(chooseClassColor);
        chooseArcherButton.setFont(start.startTitleButton);
        chooseArcherButton.addActionListener(classHandler);
        chooseArcherButton.setActionCommand("Archer");
        chooseArcherButton.setFocusPainted(false);
        chooseButtonsPanel.add(chooseArcherButton);


        start.container.add(chooseButtonsPanel);




    }



public class ClassHandler implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent ex) {
        choosenClass = ex.getActionCommand();
        averagePanel.mainPanel();


    }
}

}

huangapple
  • 本文由 发表于 2020年8月9日 05:00:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63320189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定