Results 1 to 6 of 6
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Keep running into NullPointers :/ help..

    Okay, I've revised my question. Please help if you can.

    This is the code:

    Java Code:
        public void openItemAddOnsEditor(int selectedTLindex, Products.Product.ProductModifiers pm) {
            [COLOR="red"][B]lastModGroup[/B][/COLOR] = pm.returnProduct().getModifiers().indexOf(pm);
            System.out.println("Last mod group: " + lastModGroup);
            askItemAddOnsModifierItems.removeAll();
            for (int i=0; i<pm.getSetMembers().size(); i++) {
                final Products.Product.ProductModifiers.ProductModifier mod = pm.getSetMembers().get(i);
                javax.swing.JToggleButton jt = new javax.swing.JToggleButton(mod.getDescription());
                askItemAddOnsModifierItems.add(jt);
                jt.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        javax.swing.AbstractButton ab = (javax.swing.AbstractButton) evt.getSource();
                        boolean buttonState = ab.isSelected();
                        openItemAddOnsMemberEditorAction(mod, buttonState, evt);
                    }
                });
            }
            askItemAddOnsCardsLayout.show(askItemAddOnsCards, "itemAddOnsCard2");
        }
    
        public void openItemAddOnsMemberEditorAction(
                Products.Product.ProductModifiers.ProductModifier mod,
                boolean buttonState, java.awt.event.ActionEvent evt) {
            openItemAddOnsMemberEditor(mod, buttonState);
        }
    
        public void openItemAddOnsMemberEditor(Products.Product.ProductModifiers.ProductModifier mod, boolean buttonSelected) {
            [B][U][COLOR="Red"]lastModItem[/COLOR][/U][/B] = mod.returnGroup().getSetMembers().indexOf(mod);
            System.out.println("Group: " + [COLOR="red"]mod.returnGroup().getSetDescription()[/COLOR] + "; Last mod item: " + lastModItem);
            Products.Product p = mod.returnGroup().returnProduct();
            System.out.println(p.getDescription());
            if (buttonSelected) {
                if (mod.getPrices().size() > 1) {
                    setAskModUpgradeBox(mod);
                    askModUpgrade.setVisible(true);
                }
                boolean priceByWeight = mod.getPrices().get(askModUpgradeRequest).isPricePerKg();
                if (priceByWeight) {
                    double itemWeight = 0;
                    setAskItemWeightBox(mod);
                    askItemWeight.setVisible(true);
                    itemWeight = askItemWeightRequest;
                    TransactionLineModifier tempTLM = new TransactionLineModifier(p,lastModGroup,lastModItem,askModUpgradeRequest,itemWeight);
                    askNewOrder.get(currentOrder).addTransactionLine(tempTLM);
                } else {
                    TransactionLineModifier tempTLM = new TransactionLineModifier(p,lastModGroup,lastModItem,askModUpgradeRequest);
                    askNewOrder.get(currentOrder).addTransactionLine(tempTLM);
                }
            }
        }

    This code gives me a null pointer exception in TransactionLineModifier.java because the variable lastModItem is -1.

    It is -1, I understand, because the .indexOf method didn't find anything. This is strange to me because lastModGroup is written in a similar way, however it Does find the correct object and return its (int) index.

    The other thing is that, mod.returnGroup().getSetDescription() doesn't return the correct description name... Why is that?

    When the JButtons and JToggleButtons are created and display, they display the correct Descriptions, but the problem is the Actions attached to them maybe passing the wrong objects.

    Once again, any help would be greatly appreciated.

    thanks again.
    Last edited by ozzyman; 04-22-2011 at 12:18 PM.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Updated the question/info

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I can't speak for others, but I have a hard time figuring out your problem without being able to compile/test/run/alter your code. As I've said probably too many times, an SSCCE would be best if you could create one, if not, then still the smallest possible compilable/runnable unit would be helpful.

    Much luck!

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Thanks Fu, I was too afraid to post the whole code because its really, really long; that and I thought maybe there was a conceptual problem that someone would identify.

    This is a simplified Products.java file:
    Java Code:
    // code removed
    Last edited by ozzyman; 04-22-2011 at 04:00 PM.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    I was too afraid to post the whole code because its really, really long
    Which is the point of an SSCCE, it lets you break the problem down only the necessary components (often times this is enough to discover the problem yourself) and allows one to boil a problem down to just that - it is in my opinion an essential skill in debugging (and the above is not an SSCCE).

    That being said, read the API for List, it returns -1 if an Object passed to indexOf is not found in the List. This is what I presume to be the problem, but its difficult to say give the length of code (as well as the excessive use of nested inner classes - making it hard to quickly follow)

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    thanks doWhile, and you're spot on - I discovered the reason why this is happening while I was making the SSCCE;

    Java Code:
                        /** Escape modifier */
                        public ProductModifiers returnGroup() {
                            return productModifiers.get(productModifiers.size()-1);
                        }

    This worked for me before (because when the modifiers are created, they are created in a sequence), but now it doesn't work because I need to add product modifiers to products from the GUI.

    How can I get the (ProductModifiers group object in the Product.productModifiers List) from the ProductModifier item object?

    I've done it for the ProductModifiers group like this: returning the Product its from:
    Java Code:
                /** Escape Modifiers */
                public Product returnProduct() {
                    return getProduct(productID);
                }


    EDIT:
    this is what I have:
    Java Code:
    return getProduct(productID).getModifiers().get(productModifiers.indexOf(modifierList.indexOf(this)));
    (isn't correct)


    EDIT again:

    kay, i finally got around this by adding modifier IDs and a search method.
    Last edited by ozzyman; 04-22-2011 at 03:59 PM.

Similar Threads

  1. running
    By aizen92 in forum New To Java
    Replies: 3
    Last Post: 02-11-2011, 07:50 PM
  2. Running on a Server
    By providence in forum New To Java
    Replies: 4
    Last Post: 01-31-2011, 11:11 PM
  3. Process running or not
    By smartsubroto in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 03:09 AM
  4. Replies: 0
    Last Post: 08-26-2009, 01:03 PM
  5. Running
    By AWPtic in forum Eclipse
    Replies: 1
    Last Post: 03-24-2009, 06:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •