Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9222785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:55:00+00:00 2026-06-18T03:55:00+00:00

I have 4 classes ui/ProductUI controller/ProductController project_package/Product project_package/ProductRecords When I try to push the

  • 0

I have 4 classes

ui/ProductUI

controller/ProductController

project_package/Product

project_package/ProductRecords

When I try to push the button “add” it returns me an error, which if I run it through the controller doesn’t show. and I put the two same identical requests.

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;
    }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:55:02+00:00Added an answer on June 18, 2026 at 3:55 am

    The full error is this

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

    thrown on this line

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

    due to the fact that productCtrl has not been instantiated. You could do:

    productCtrl = new ProductController();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got to classes Product and Store which have many to many relation I
I have a product entity which has several classes (each product type has different
I have added the Authentication attribute on controller classes which are for admin purposes
I have 2 classes which get saved in the google datastore. class product which
I have three classes which are connected to each other. Order, OrderDetail and Product.
I have in my javascript these 2 functions classes: // product class function Product()
I have classes which have automatic properties only like public customerName {get; set;}. They
I have two ActiveRecord classes class Product : ActiveRecordBase<Product> { private IList<Usage> _usages =
I have these 3 classes: public class Product { public int ProductId { get;
I have 2 classes (summarized for brevity) : public class Product : Entity<Guid> {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.