使用Jpanel的Java NullPointer异常[重复]

时间:2023-01-28 15:25:19

This question already has an answer here:

这个问题在这里已有答案:

Hello guys I started a personal project to where I would basically make a meal planner based of how many calories a person wants to consume per day for myself. I know it maybe a tough challenge for me since I'm a beginner, but I can do it! I will leave the backend later, but for now I wanted to work on the UI and I'm having trouble with this piece of code. I want to make a list of panels to be held in a panel, based on how many number of meals they want is how many panels will appear. any insight on this will be much appreciated.

大家好,我开始了一个个人项目,我基本上可以制作一个膳食计划器,根据每个人每天要消耗多少卡路里。我知道这对我来说可能是一个艰难的挑战,因为我是初学者,但我能做到!我稍后会离开后端,但是现在我想在UI上工作,我在使用这段代码时遇到了麻烦。我想列出一个面板列表,根据他们想要的用餐数量是多少面板出现。任何有关这方面的见解将非常感激。

package mealplanner;

import javax.swing.*;

/**
 * Created by Usman on 6/8/2017.
 */
public class MealPlannerPanel extends JPanel {

    JPanel[] panel;
    int mealsPerDay, caloriesPerDay;
    public MealPlannerPanel(){
        mealsPerDay = Integer.parseInt(JOptionPane.showInputDialog("How many meals would you like per day?",null));
        caloriesPerDay = Integer.parseInt(JOptionPane.showInputDialog("What is your daily calorie aim?",null));
        panel = new JPanel[mealsPerDay];
        for(int i = 0; i < panel.length; i++){
            add(panel[i]);
        }
    }
    public static void main(String[] args){
        MealPlannerPanel planner = new MealPlannerPanel();
        JFrame frame = new JFrame("Meal Planner");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(planner);
        frame.pack();
        frame.setVisible(true);
    }
}

1 个解决方案

#1


1  

panel = new JPanel[mealsPerDay];

That statement just creates an array that is able to hold the given number of panels.

该语句只是创建一个能够容纳给定数量的面板的数组。

It does not actually create the panel (so when you index into the array you will get a null object).

它实际上并不创建面板(因此当您索引数组时,您将获得一个null对象)。

So you need something like:

所以你需要这样的东西:

    for(int i = 0; i < panel.length; i++){
        JPanel onePanel = new JPanel();
        panel[i] = onePanel;
        add(panel[i]);
    }

Also, be more descriptive with you variable names. "panel" implies a single component. Given it is meant to represent an array the least you can do is call it "panels" so we know there is more than one.

另外,对变量名称更具描述性。 “面板”意味着单个组件。鉴于它意味着代表一个数组,你可以做的最少就是称它为“面板”,所以我们知道不止一个。

#1


1  

panel = new JPanel[mealsPerDay];

That statement just creates an array that is able to hold the given number of panels.

该语句只是创建一个能够容纳给定数量的面板的数组。

It does not actually create the panel (so when you index into the array you will get a null object).

它实际上并不创建面板(因此当您索引数组时,您将获得一个null对象)。

So you need something like:

所以你需要这样的东西:

    for(int i = 0; i < panel.length; i++){
        JPanel onePanel = new JPanel();
        panel[i] = onePanel;
        add(panel[i]);
    }

Also, be more descriptive with you variable names. "panel" implies a single component. Given it is meant to represent an array the least you can do is call it "panels" so we know there is more than one.

另外,对变量名称更具描述性。 “面板”意味着单个组件。鉴于它意味着代表一个数组,你可以做的最少就是称它为“面板”,所以我们知道不止一个。