1

私は4つのクラスを持っています

ui/ProductUI

コントローラ/製品コントローラ

project_package/製品

project_package/ProductRecords

「追加」ボタンを押そうとするとエラーが返されますが、コントローラーを介して実行しても表示されません。2つの同じ同じリクエストを入れました。

UI:

    package ui;

import java.awt.event.*;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UnsupportedLookAndFeelException;

import controller.ProductController;

public class ProductUI {

    private JFrame f;
    private JPanel page1, page2;
    private JTabbedPane tp;
    private JTextField addProductTextFieldName;
    private JTextField addProductTextFieldPrice;
    private JTextField addProductTextFieldDescription;
    private JTextField addProductTextFieldDiscount;
    private JTextField addProductTextFieldQuantity;
    private ProductController productCtrl;  

    public void display()
    {
        f.getContentPane().add(tp);
        tp.add("Add product", page1);
        tp.add("Edit product", page2);
        f.setSize(510,450);
        f.setVisible(true);
        f.setResizable(false);
    }

    public ProductUI()
    {

        f = new JFrame("Product UI");
        tp = new JTabbedPane();


        //**********************************
        // PAGE 1
        //**********************************

        page1 = new JPanel();

        JLabel addProductLabelName = new JLabel("Product Name");
        JLabel addProductLabelPrice = new JLabel("Product Price");      
        JLabel addProductLabelDescription = new JLabel("Product Description");      
        JLabel addProductLabelDiscount = new JLabel("Product Discount");        
        JLabel addProductLabelQuantity = new JLabel("Product Quantity");        
        JLabel addProductLabelLocation = new JLabel("Product Location");
        JCheckBox addProductCheckBoxAalborg = new JCheckBox("Aalborg");     
        JCheckBox addProductCheckBoxOdense = new JCheckBox("Odense");       
        JCheckBox addProductCheckBoxCopenhagen = new JCheckBox("Copenhagen");       
        JButton addProductButtonReset = new JButton("Reset");       
        JButton addProductButtonAdd = new JButton("Add");
        JButton addProductButtonClose = new JButton("Close window");
        JButton addProductButtonExit = new JButton("Exit");

        addProductTextFieldName = new JTextField(10);
        addProductTextFieldPrice = new JTextField(10);
        addProductTextFieldDescription = new JTextField(10);
        addProductTextFieldDiscount = new JTextField(10);
        addProductTextFieldQuantity = new JTextField(10);


        addProductButtonAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                /*double productPrice = Double.parseDouble(addProductTextFieldPrice.getText());
                double productDiscount = Double.parseDouble(addProductTextFieldDiscount.getText());
                int productQuantity = Integer.parseInt(addProductTextFieldQuantity.getText());
                String productName = addProductTextFieldName.getText();
                String productDescription = addProductTextFieldDescription.getText();
                //productCtrl.createProduct(productName, productPrice, productDiscount, productDescription, productQuantity, "location");
                */
                productCtrl.createProduct("Test", 20.0, 20.0, "haha", 20, "nqkude");
            }   
        });


        GroupLayout gl_page1 = new GroupLayout(page1);
        gl_page1.setHorizontalGroup(
            gl_page1.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_page1.createSequentialGroup()
                    .addGap(95)
                    .addGroup(gl_page1.createParallelGroup(Alignment.TRAILING)
                        .addGroup(gl_page1.createSequentialGroup()
                            .addGroup(gl_page1.createParallelGroup(Alignment.LEADING)
                                .addComponent(addProductLabelName)
                                .addComponent(addProductLabelPrice)
                                .addComponent(addProductLabelDescription)
                                .addComponent(addProductLabelDiscount)
                                .addComponent(addProductLabelQuantity)
                                .addComponent(addProductLabelLocation))
                            .addGap(82))
                        .addGroup(gl_page1.createSequentialGroup()
                            .addGap(34)
                            .addGroup(gl_page1.createParallelGroup(Alignment.TRAILING)
                                .addComponent(addProductButtonAdd, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                                .addComponent(addProductButtonClose, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
                            .addGap(40)))
                    .addGroup(gl_page1.createParallelGroup(Alignment.LEADING, false)
                        .addComponent(addProductCheckBoxOdense)
                        .addComponent(addProductCheckBoxAalborg)
                        .addComponent(addProductTextFieldName, GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                        .addComponent(addProductTextFieldPrice)
                        .addComponent(addProductTextFieldDescription)
                        .addComponent(addProductTextFieldDiscount)
                        .addComponent(addProductTextFieldQuantity)
                        .addGroup(gl_page1.createSequentialGroup()
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addGroup(gl_page1.createParallelGroup(Alignment.TRAILING, false)
                                .addComponent(addProductButtonExit, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(addProductCheckBoxCopenhagen, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(addProductButtonReset, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
                            .addGap(25)))
                    .addGap(100))
        );
        gl_page1.setVerticalGroup(
            gl_page1.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_page1.createSequentialGroup()
                    .addGap(58)
                    .addGroup(gl_page1.createParallelGroup(Alignment.BASELINE)
                        .addComponent(addProductLabelName)
                        .addComponent(addProductTextFieldName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addGroup(gl_page1.createParallelGroup(Alignment.BASELINE)
                        .addComponent(addProductLabelPrice)
                        .addComponent(addProductTextFieldPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addGroup(gl_page1.createParallelGroup(Alignment.BASELINE)
                        .addComponent(addProductLabelDescription)
                        .addComponent(addProductTextFieldDescription, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addGroup(gl_page1.createParallelGroup(Alignment.BASELINE)
                        .addComponent(addProductLabelDiscount)
                        .addComponent(addProductTextFieldDiscount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addGroup(gl_page1.createParallelGroup(Alignment.BASELINE)
                        .addComponent(addProductLabelQuantity)
                        .addComponent(addProductTextFieldQuantity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addGroup(gl_page1.createParallelGroup(Alignment.BASELINE)
                        .addComponent(addProductLabelLocation)
                        .addComponent(addProductCheckBoxAalborg))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_page1.createParallelGroup(Alignment.TRAILING)
                        .addGroup(gl_page1.createSequentialGroup()
                            .addComponent(addProductCheckBoxOdense)
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addComponent(addProductCheckBoxCopenhagen)
                            .addGap(7)
                            .addComponent(addProductButtonReset))
                        .addComponent(addProductButtonAdd))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_page1.createParallelGroup(Alignment.BASELINE)
                        .addComponent(addProductButtonClose)
                        .addComponent(addProductButtonExit))
                    .addContainerGap(57, Short.MAX_VALUE))
        );
        page1.setLayout(gl_page1);

        //**********************************
        // PAGE 2
        //**********************************

        page2 = new JPanel();
        GroupLayout gl_page2 = new GroupLayout(page2);
        gl_page2.setHorizontalGroup(
            gl_page2.createParallelGroup(Alignment.LEADING)
                .addGap(0, 499, Short.MAX_VALUE)
        );
        gl_page2.setVerticalGroup(
            gl_page2.createParallelGroup(Alignment.LEADING)
                .addGap(0, 394, Short.MAX_VALUE)
        );
        page2.setLayout(gl_page2);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException 
    {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        ProductUI productMenu = new ProductUI();
        productMenu.display();
    }
}

package controller;

import project_package.*;

public class ProductController {

    ProductRecords productRecordsCtrl;

    public ProductController()
    {
        productRecordsCtrl = ProductRecords.getInstance();
    }

    public void createProduct(String name, double price, double discount, String description, int quantity, String location)
    {
        productRecordsCtrl.createProduct(name, price, discount, description, quantity, location);
    }
    }

package project_package;

import java.util.ArrayList;
//import java.util.Arrays;
import java.util.List;

public class ProductRecords 
{
    private static ProductRecords instance = null;
    ArrayList<Product> products = new ArrayList<Product>();
    List<Integer> quantity = new ArrayList<Integer>();
    List<String> location = new ArrayList<String>();
    int productIndex = 0;

    public static ProductRecords getInstance()
    {
        if(instance == null)
        {
            instance = new ProductRecords();
        }
        return instance;
    }

    // add a product to the list
    public void addProduct(Product product, int quantity, String location)
    {
        products.add(product.getID(), product);
        this.quantity.add(product.getID(), quantity);
        this.location.add(product.getID(), location);
    }

    // create (add) a new product (to the shop list)
    public void createProduct(String name, double price, double discount, String description, int quantity, String location)
    {
        Product newProduct = new Product(productIndex, price, discount, name, description);
        addProduct(newProduct, quantity, location);
        productIndex++;
    }
}

package project_package;
public class Product {

    int id;
    double price;
    double discount;
    String name;
    String description;

    public Product(int id, double price, double discount, String name, String description)
    {
        this.id = id;
        this.price = price;
        this.discount = discount;
        this.name = name;
        this.description = description;
    }
    }
4

1 に答える 1

4

完全なエラーはこれです

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

この行にスローされます

productCtrl.createProduct("Test", 20.0, 20.0, "haha", 20,"nqkude");

productCtrlインスタンス化されていないためです。あなたがすることができます:

productCtrl = new ProductController();
于 2013-02-02T00:08:14.780 に答える