7 Replies - 604 Views - Last Post: 28 June 2010 - 03:16 PM Rate Topic: -----

#1 Guest_Ana*


Reputation:

Null Pointer Exception w/ Inventory Program

Posted 27 June 2010 - 10:33 PM

Hey everyone. I've been trying my best not to ask a question, but I just can't help it. I'm working on an assignment thats due at 3am EST and am totally stuck. I'm getting a Null Pointer Exception when I click any of the buttons I created (except for the exit one). To give a little more background info, this is what my assigment is (sorry this post is a little lengthy):

Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.

Add a company logo to the GUI using Java graphics classes.

So this is the code I have:

package inventoryprogram3;

/**
 *
 * @author Ana
 */
public class InventoryProgram3 {

//main method begins execution of application
    
   	public static void main(String args []) {
		Inventory invent = new Inventory(); //creates an array
		Movie dvd;

		dvd = new Movie("Drama", "Fear and Loathing in Las Vegas", 1001, 16.95, 15); //item 1
		invent.add(dvd);
		dvd = new Movie("Animation/Comedy", "The Nine Lives of Fritz the Cat", 1002, 23.95, 2); //item 2
		invent.add(dvd);
		dvd = new Movie("Crime/Thriller", "Silence of the Lambs", 1003, 16.95, 10); //item 3
		invent.add(dvd);
		dvd = new Movie("Drama", "Sling Blade", 1004, 16.95, 5); //item 4
		invent.add(dvd);

		invent.display();

	} //end main

} // end class InventoryProgram3



package inventoryprogram3;

/**
 *
 * @author Ana
 */
class DVD { //priavte variable containers
        private int dvdNumber;
        private String dvdTitle;
        private int dvdStock;
        private double dvdPrice;

        public DVD( String title, int item, double price, int stock) { //four argument constructor
        dvdNumber = item;
        dvdTitle = title;
        dvdStock = stock;
        dvdPrice = price;

        } //end four argument constructor

        // set DVD Item number
        public void setDvdNumber(int item) {
                dvdNumber = item;
        } //end method  set Dvd Item number

        //return DVD Item number
        public int getDvdNumber() {
                return dvdNumber;
        } //end method get Dvd Item number

        //set DVD Title
        public void setDvdTitle(String title) {
                dvdTitle = title;
        } //end method set Dvd Title

        //return Dvd Title
        public String getDvdTitle() {
                return dvdTitle;
        } //end method get Dvd Title

        public void setDvdStock(int stock) {
                dvdStock = stock;
        } //end method set Dvd Stock

        //return dvd Stock
        public int getDvdStock() {
                return dvdStock;
        } //end method get Dvd Stock

        public void setDvdPrice(double price) {
                dvdPrice = price;
        } //end method setdvdPrice

        //return DVD Price
        public double getDvdPrice() {
                return dvdPrice;
        } //end  method get Dvd Price

        //calculate inventory value
        public double value() {
                return dvdPrice * dvdStock;
        } //end method value

        public String toString() { //display
                return String.format("ITEM #: %3d\n TITLE: %-20s\n UNITS IN STOCK: %3d\n PRICE: $%6.2f\n VALUE OF INVENTORY: $%7.2f\n",
                                                          dvdNumber, dvdTitle, dvdStock, dvdPrice, value());
        } // end display
        
         // Simply gets the base class's value, and figures out the 5% restocking fee only
        public double restockingFee() {
                return value() * 0.05f;
        }

       
        
} //end class DVD


class Movie extends DVD { //subclass
        private String movieGenre; //private variable container
       

        public Movie(String genre, String title, int item, double price, int stock) { //5 argument constructor
                super(title, item, price, stock);
                movieGenre = genre;
        } //end five argument constructor

        public String getMovieGenre() {
            return movieGenre;
        }

        // Overrides value() in Movie class by calling the base class version and
        // adding a 5% restocking fee on top
         public double value() {
                return super.value() + restockingFee();
        }

        // Simply gets the base class's value, and figures out the 5% restocking fee only
        public double restockingFee() {
                return super.value() * 0.05f;
        }

        

        public String toString() { //add unique feature to string
                String s = String.format("\n GENRE: %-12s\n", movieGenre);
                s = s + " " + super.toString();
                return s;
        } //end method

} // end class Movie




package inventoryprogram3;

/**
 *
 * @author Ana
 */
class Inventory { //private variable containers
        private DVD[] dvds;
        private int count;

        Inventory() {
                dvds = new DVD[10];
                count = 0; 
        }

        public void add(DVD dvd) { 
                dvds[count] = dvd;
                ++count;
                sort();
        }

        public double entireValue() { //calculate entirevalue
                double value = 0;
                for (int i = 0; i < count; i++) {
                        value = value + dvds[i].value();
                }
                return value;
        } // end method entire value

        public void sort() {
                for (int index = 1; index < count; index++) {
                        DVD key = dvds[index];
                        int position = index;

                        //  Shift larger values to the right
                        while (position > 0 && key.getDvdTitle().compareTo(dvds[position-1].getDvdTitle()) < 0) {
                                dvds[position] = dvds[position-1];
                                position--;
                        } //end while
                        dvds[position] = key;
                } // end for
        } //end method sort

        public Movie get(int i) {
		return get(i);
	}


        public int size() {
		return size();
	}

        public void display() { //display
                System.out.println("\nTHERE ARE CURRENTLY " + count + " TITLES IN INVENTORY\n");
                for (int i = 0; i < count; i++)
                        System.out.printf("%3d  %s\n", i, dvds[i]);
                System.out.printf("\nTOTAL VALUE: $%.2f\n\n", entireValue());
                System.out.println("note: value of inventory and total value fields include 5% restocking fee.");
        } //display


} // end class Inventory




package inventoryprogram3;

/**
 *
 * @author Ana
 */
public class InventoryGUI extends javax.swing.JFrame {

    private Inventory inv;
    private int currentDisplay = 0;
    
    /** Creates new form InventoryGUI */
    public InventoryGUI() {
        initComponents();
    }
    
    public void init() {
      
                Movie p1 = new Movie("Drama", "Fear and Loathing in Las Vegas", 1001, 16.95, 15);
		Movie p2 = new Movie("Animation/Comedy", "The Nine Lives of Fritz the Cat", 1002, 23.95, 2);
		Movie p3 = new Movie("Crime/Thriller", "Silence of the Lambs", 1003, 16.95, 10);
		Movie p4 = new Movie("Drama", "Sling Blade", 1004, 16.95, 5);


		inv = new Inventory();
		inv.add(p1);
		inv.add(p2);
		inv.add(p3);
                inv.add(p4);

		inv.sort();

//	Output

		for (int i = 0; i < inv.size(); i++) {
			System.out.println("GENRE: " + inv.get(i).getMovieGenre());
                        System.out.println("ITEM #: " + inv.get(i).getDvdNumber());
                        System.out.println("NAME: " + inv.get(i).getDvdTitle());
			System.out.println("UNITS IN STOCK: " + inv.get(i).getDvdStock());
			System.out.println("PRICE: $" + String.format("%.2f",inv.get(i).getDvdPrice()));
			System.out.println("VALUE OF INVENTORY: $" + String.format("%.2f",inv.get(i).value()));
			System.out.println("RESTOCKING FEE: $" + String.format("%.2f",inv.get(i).restockingFee()));
			System.out.println();
		}

		//total
		System.out.println("TOTAL VALUE: $" + String.format("%.2f",inv.entireValue()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setText("Welcome to the DVD Inventory Program!");
        jScrollPane1.setViewportView(jTextArea1);

        jButton2.setText("Previous");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Next");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Last");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Exit");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton1.setText("First");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Ana\\Desktop\\bulkdiscstack.jpg")); // NOI18N
        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 338, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton3)
                .addGap(14, 14, 14)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 374, Short.MAX_VALUE)
                .addComponent(jButton5)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton5))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton4)
                            .addComponent(jButton2)
                            .addComponent(jButton3))))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
         System.exit(0); //exit program
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
//action performed by clicking first button
        
        currentDisplay = 0;// go to the beginning
	displayProduct();
        
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
//action performed by clicking previous button        

if (currentDisplay > 0) {
                    currentDisplay--;
                }
				else {
                    currentDisplay = inv.size() - 1;
                }
				displayProduct();

    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
//action performed by clicking next button

        if (currentDisplay < inv.size()-1) {
                    currentDisplay++;
                } //advance to the end
				else {
                    currentDisplay = 0;
                }
				displayProduct();

    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
//action performed by clicking last button

        currentDisplay = inv.size()-1;
		displayProduct();

    }                                        


 
// display
	public void displayProduct() {
		jTextArea1.setText("Product Details:\n");
                jTextArea1.append("GENRE: " + inv.get(currentDisplay).getMovieGenre() + "\n");
		jTextArea1.append("ITEM #: " + inv.get(currentDisplay).getDvdNumber() + "\n");
		jTextArea1.append("TITLE: " + inv.get(currentDisplay).getDvdTitle() + "\n");
		jTextArea1.append("UNITS IN STOCK: " + inv.get(currentDisplay).getDvdStock() + "\n");
		jTextArea1.append("PRICE: $" + String.format("%.2f",inv.get(currentDisplay).getDvdPrice()) + "\n");
		jTextArea1.append("VALUE OF INVENTORY: $" + String.format("%.2f",inv.get(currentDisplay).value()) + "\n");
		jTextArea1.append("RESTOCKING FEE: $" + String.format("%.2f",inv.get(currentDisplay).restockingFee()) + "\n\n");

		jTextArea1.append("TOTAL VALUE: $" + String.format("%.2f",inv.entireValue()));

	}


    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InventoryGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration


}




Now that that's out of the way, these are my errors upon clicking the "FIRST" button:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at inventoryprogram3.InventoryGUI.displayProduct(InventoryGUI.java:223)
at inventoryprogram3.InventoryGUI.jButton1ActionPerformed(InventoryGUI.java:180)
at inventoryprogram3.InventoryGUI.access$400(InventoryGUI.java:18)
at inventoryprogram3.InventoryGUI$5.actionPerformed(InventoryGUI.java:119)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

and the "PREVIOUS" button:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at inventoryprogram3.InventoryGUI.jButton2ActionPerformed(InventoryGUI.java:191)
at inventoryprogram3.InventoryGUI.access$000(InventoryGUI.java:18)
at inventoryprogram3.InventoryGUI$1.actionPerformed(InventoryGUI.java:91)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


and the "NEXT" button:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at inventoryprogram3.InventoryGUI.jButton3ActionPerformed(InventoryGUI.java:200)
at inventoryprogram3.InventoryGUI.access$100(InventoryGUI.java:18)
at inventoryprogram3.InventoryGUI$2.actionPerformed(InventoryGUI.java:98)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


and finally, the "LAST" button:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at inventoryprogram3.InventoryGUI.jButton4ActionPerformed(InventoryGUI.java:213)
at inventoryprogram3.InventoryGUI.access$200(InventoryGUI.java:18)
at inventoryprogram3.InventoryGUI$3.actionPerformed(InventoryGUI.java:105)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


again, i apologize for the lengthy post. i hope i did it right. ;) please have patience with me, i've been working on this for about 7 hours now. pfft.

Is This A Good Question/Topic? 0

Replies To: Null Pointer Exception w/ Inventory Program

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Null Pointer Exception w/ Inventory Program

Posted 27 June 2010 - 10:52 PM

What looks like is happening is that the DVD[] in your inventory class has ten slots, but only 4 DVDs. So when you access anything outside of index 3, you get the NullPointerExceptions.

Also, you really shouldn't use the NetBeans GUI Generator (or any GUI Generator with Java), as they produce cumbersome, inefficient, and unmaintainable code for humans. Swing is so easy to use, that you should write your GUIs by hand instead. We have plenty of Swing Tutorials in the Java tutorials section, if you want to check them out.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Ana*


Reputation:

Re: Null Pointer Exception w/ Inventory Program

Posted 27 June 2010 - 11:18 PM

thanks so much for taking a look at what the heck is going on with the code! i've been looking at this screen for so long not much is making sense at this point. anyways, i changed the 10 slots to 4 slots, but to no avail. it still gives me the same errors too. i'm thinking it has something to do with the CurrentDisplay in InventoryGUI class but i can't be sure. and if it is, i have no idea how to fix it. everything looks as it should from my end. stupid null pointer exception. *grumble*
Was This Post Helpful? 0

#4 Guest_Ana*


Reputation:

Re: Null Pointer Exception w/ Inventory Program

Posted 28 June 2010 - 12:06 AM

hmmm.. maybe I fixed something by messing around, but now I have a stack overflow error..eep.

and it directs me to this:

 public int size() {
		return this.size();
	}



how would i correct that?
Was This Post Helpful? 0

#5 fallenreaper  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 240
  • Joined: 19-June 10

Re: Null Pointer Exception w/ Inventory Program

Posted 28 June 2010 - 11:23 AM

future thought....

It seems that you had posted your entire program, which is never a good thing. Try to post specific parts of your code only.... such as the information that pertains specifically to the problem asked. :) That can be alot for us to read through. :) We will do it, but.... can be cumbersome
Was This Post Helpful? -3
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Null Pointer Exception w/ Inventory Program

Posted 28 June 2010 - 12:03 PM

@Ana: You get a StackOverflowError b/c you recurse infinitely. Within the size() method, you invoke the size() method without a base case. Do you want to return the length of the DVD[] in your Inventory class instead?

@fallenreaper: A lot of times, the error stems from other parts of the code not highlighted by the error messages. And as we are dealing with 3 classes, not 30 classes, posting all the code is fine.
Was This Post Helpful? 0
  • +
  • -

#7 fallenreaper  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 240
  • Joined: 19-June 10

Re: Null Pointer Exception w/ Inventory Program

Posted 28 June 2010 - 03:03 PM

Yea, the classes, but i was thinking we werent needing to know all of the functions within the classes.....mostly within the DVD class as well as the class Movie extends DVD. my bad. I always try to make my stuff i post short and as sweet as possibly to minimize time people need to spend reading and studying the code. >_>

*shrug* personal preference for that and the idea that you should try to never post all your code as classmates *may* see this or use the same site and be tempted to steal code, causing a Fail for the project they were here helping for in the first place.

*shrug* I guess i should not worry about that sort of thing, i try to go to bed at night happily knowing that the people we were helping don't get caught in plagiarism with a classmate. ;) Thats all, just trying to be helpful.
Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Null Pointer Exception w/ Inventory Program

Posted 28 June 2010 - 03:16 PM

How are we going to help debug if we don't know how the classes work? In this case, minimizing the time we spend reading the posts is going to minimize how helpful we can be. I've seen it too many times to count. And if you are really that concerned about plagiarism, asking for help online isn't the thing to do. As long as users show some effort and describe their problems, I'm willing to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1