CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2011
    Posts
    3

    I'm sure it is really simple but it is driving me crazy

    I am not a great programmer but I need to make a simple rule management system, the problem I am having is that due to the number of options I had to divide it up over two separate JFrames, what I have done so far is that when the' Next' button is clicked it will combine the users input into a single String which works fine but I need to access this String from the new class on the second JFrame at this point it seems what ever I try I end up with a null-point exception.

    This is the code for my button (not including any of my poor attempts to pass it over!):

    private void cmdnextActionPerformed(java.awt.event.ActionEvent evt) {
    ruleHeader = (String)lsbaction.getSelectedItem() + " " + (String)lsbprotocol.getSelectedItem();

    if (cbsourceipany.isSelected()){
    ruleHeader = ruleHeader + " any";
    }else{
    ruleHeader = ruleHeader + " " + txtsource.getText() + (String)lsbsourcex.getSelectedItem();
    }
    if (cbsourceportany.isSelected()){
    ruleHeader = ruleHeader + " any";
    }else{
    ruleHeader = ruleHeader + " " + txtsport.getText();
    }

    if (cbdirect.isSelected()) {
    ruleHeader = ruleHeader + " <> ";
    }else{
    ruleHeader = ruleHeader + " -> ";
    }

    if (cbdestipany.isSelected()){
    ruleHeader = ruleHeader + " any";
    }else{
    ruleHeader = ruleHeader + " " + txtdest.getText() + (String)lsbdestx.getSelectedItem();
    }
    if (cbdestportany.isSelected()){
    ruleHeader = ruleHeader + " any";
    }else{
    ruleHeader = ruleHeader + " " + txtdestport.getText();
    }
    txttest1.setText(ruleHeader);

    new Rule_Addition_Options().setVisible(true);
    }

  2. #2
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: I'm sure it is really simple but it is driving me crazy

    Hey Ademske,

    Let's see if a new poster can help another new poster here. =D

    First off you should read the posting rules on the forum

    http://www.codeguru.com/forum/showthread.php?t=217745

    The rules are pretty short and it helps to keep everything uniform.

    Now about your problem.

    You say you are getting a null pointer exception. Can you trace the exception and find out what called the method that got the exception? That would help track it.

    If you know what is throwing the NullPointerException (NPE) you can search the Java API and find out when/why that type of object throws a NPE. For example, if it is one of your JFrame objects throwing a NPE then you can see that a JFrame only throws a NPE when you try to call the remove() method when there are no elements in the JFrame.

    Also you mentioned multiple JFrame objects. Are you trying to concatenate Strings from different components from different JFrame objects? That seems odd to me. Do you need multiple JFrame objects or could you combine your components into a single JFrame?

    Anyway a bit more info and we might be able to put this one to bed.

    Happy hunting.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: I'm sure it is really simple but it is driving me crazy

    Quote Originally Posted by djgarrison
    Let's see if a new poster can help another new poster here.
    Good to see some new help on the site. I'm not going to be around much over the next few weeks so feel free to answer as many questions as you want
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Jan 2011
    Posts
    3

    Re: I'm sure it is really simple but it is driving me crazy

    Hi djgarrison,

    Thanks for the reply, The NullPointerException is not from any of my JFrame objects I always hit it when I try to access the "ruleHeader" String from my second JFrame. The reason I have multiple JFrames is because there are about 20 different inputs that need to be combined into a single String but they wont all fix on a single page. it might be easier to explain if I include more of my code.

    **Rule_Addition**

    Code:
     package Rule_Management_GUI;
    
    public class Rule_Addition extends javax.swing.JFrame {
        //private Rule_One rulz;
    
        public String ruleHeader;
    
        /** Creates new form Rule_Addition */
        public Rule_Addition() {
            initComponents();
    
    
        }                                      
    
        private void cmdnextActionPerformed(java.awt.event.ActionEvent evt) {                                        
            ruleHeader = (String) lsbaction.getSelectedItem() + " " + (String) lsbprotocol.getSelectedItem();
    
            if (cbsourceipany.isSelected()) {
                ruleHeader = ruleHeader + " any";
            } else {
                ruleHeader = ruleHeader + " " + txtsource.getText() + (String) lsbsourcex.getSelectedItem();
            }
            if (cbsourceportany.isSelected()) {
                ruleHeader = ruleHeader + " any";
            } else {
                ruleHeader = ruleHeader + " " + txtsport.getText();
            }
    
            if (cbdirect.isSelected()) {
                ruleHeader = ruleHeader + " <> ";
            } else {
                ruleHeader = ruleHeader + " -> ";
            }
    
            if (cbdestipany.isSelected()) {
                ruleHeader = ruleHeader + " any";
            } else {
                ruleHeader = ruleHeader + " " + txtdest.getText() + (String) lsbdestx.getSelectedItem();
            }
            if (cbdestportany.isSelected()) {
                ruleHeader = ruleHeader + " any";
            } else {
                ruleHeader = ruleHeader + " " + txtdestport.getText();
            }
            txttest1.setText(ruleHeader);
            // rulz.setRule("Hello my name is Adam");
            new Rule_Addition_Options().setVisible(true);
        }
    **Rule_Addition_Options**
    Code:
     package Rule_Management_GUI;
    
    public class Rule_Addition_Options extends Rule_Addition {
    
        private Rule_Addition rulz;
    
        public Rule_Addition_Options() {
            initComponents();
            txtmsg.setEnabled(false);
            txtlogto.setEnabled(false);
            txtminfrag.setEnabled(false);
            txtttl.setEnabled(false);
            txtid.setEnabled(false);
            txtdsize.setEnabled(false);
            txtcontent.setEnabled(false);
            txtoffset.setEnabled(false);
    
            String test = (String) rulz.ruleHeader;
            txttest.setText(test);
       }                                 
    
        private void cmbnextActionPerformed(java.awt.event.ActionEvent evt) {                                        
            new Rule_Addition_Options_pg2().setVisible(true);
            new Rule_Addition_Options().setVisible(false);
        }                                       
    
        private void cbmsgActionPerformed(java.awt.event.ActionEvent evt) {                                      
            if (cbmsg.isSelected()) {
                txtmsg.setEnabled(true);
            } else {
                txtmsg.setEnabled(false);
            }
        }                                     
    
        private void cblogtoActionPerformed(java.awt.event.ActionEvent evt) {                                        
            if (cblogto.isSelected()) {
                txtlogto.setEnabled(true);
            } else {
                txtlogto.setEnabled(false);
            }
        }                                       
    
        private void cbminfragActionPerformed(java.awt.event.ActionEvent evt) {                                          
            if (cbminfrag.isSelected()) {
                txtminfrag.setEnabled(true);
            } else {
                txtminfrag.setEnabled(false);
            }
        }                                         
    
        private void cbttlActionPerformed(java.awt.event.ActionEvent evt) {                                      
            if (cbttl.isSelected()) {
                txtttl.setEnabled(true);
            } else {
                txtttl.setEnabled(false);
            }
        }                                     
    
        private void cbidActionPerformed(java.awt.event.ActionEvent evt) {                                     
            if (cbid.isSelected()) {
                txtid.setEnabled(true);
            } else {
                txtid.setEnabled(false);
            }
        }                                    
    
        private void cbdsizeActionPerformed(java.awt.event.ActionEvent evt) {                                        
            if (cbdsize.isSelected()) {
                txtdsize.setEnabled(true);
            } else {
                txtdsize.setEnabled(false);
            }
        }                                       
    
        private void cbcontentActionPerformed(java.awt.event.ActionEvent evt) {                                          
            if (cbcontent.isSelected()) {
                txtcontent.setEnabled(true);
            } else {
                txtcontent.setEnabled(false);
            }
        }                                         
    
        private void cboffsetActionPerformed(java.awt.event.ActionEvent evt) {                                         
            if (cboffset.isSelected()) {
                txtoffset.setEnabled(true);
            } else {
                txtoffset.setEnabled(false);
            }
        }
    So Basically I need to access the String "ruleHeader" from the "Rule_Addition_Options" page so that I can combine the inputs and later write them to a file but any technique I try I always get a nullpointexception

  5. #5
    Join Date
    Jan 2011
    Posts
    3

    Re: I'm sure it is really simple but it is driving me crazy

    Thanks for your help djgarrison but I have it working now! it seems that the reason I couldn't get any techniques to work (like when I tried to use the Getter/Setter methods) was because I didn't have a constructor! I am such an idiot and way out of practice! lol :P

    Thanks All

  6. #6
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: I'm sure it is really simple but it is driving me crazy

    Good times. Sometimes just having to explain it explicitly to someone else lets you weed out your own mistakes. Glad you are moving forward.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured