1 2 3 Previous Next 33 Replies Latest reply: Mar 9, 2013 9:34 PM by EJP RSS

    Hash Mapping

    995566
      Hi, i am trying to get this Working but cant seem to figure out what i need to do to get it to Work.

      When the Connection Button is Clicked its giving me a Null Pointer Exception on the textField_ID.setText(" " + currentEntry.getsID() );
      It Seem to Me that its giving me a Null as it can not pick up the Text Field on the Main Class from the StaticMap how can i over come this
      public class StaticMap 
      {
           
              
            static Person currentEntry;
              static Queries Queries;
              static List<Person> results; 
               static int numberOfEntries = 0;
            static int currentEntryIndex;
             
      
                 static JTextField TextField_Id;
                      static JTextField indexTextField;
                static JTextField maxTextField;
           
      
      
              
           public static Map <String, ActionListener> listener = new HashMap <String, ActionListener> ();
           static 
      
           {
                
                doctorQueries = new DoctorQueries();
                
             listener.put("CONNECT", new ActionListener() {
      
                
                     @Override
                     
                     public  void actionPerformed(ActionEvent e) 
                     {
                           ConnectButtonActionPerformed( e );
                     }
                  });
      
      public static  void ConnectButtonActionPerformed( ActionEvent evt )
              {
                   
                      try
                      {
                         results = Queries.getAllPeople();
                    
                         
                            
                        numberOfEntries = results.size();
                      
                     
                         if ( numberOfEntries != 0 )
                         {
                            currentEntryIndex = 0;
                            currentEntry = results.get( currentEntryIndex );
      
                            textField_ID.setText(" " + currentEntry.getsID() );
                            maxTextField.setText( " " + numberOfEntries );
                            indexTextField.setText( " " + ( currentEntryIndex + 1 ) );
                            nextButton.setEnabled( true );
                            previousButton.setEnabled( true ); 
                         
                         } // end if 
                       
                      } // end try
                      catch ( Exception e )
                      {
                         e.printStackTrace();
                      } // end catch
                     
                   } // end method browseButtonActionPerformed
      
             
      public class Display extends JFrame  
      {
      
              
                 static Person currentEntry;
                   static Queries Queries;
                   static List<Person> results; 
                    static int numberOfEntries = 0;
                 static int currentEntryIndex;
      
      
                /*
                 * Setting up Private Constructors 
                 */
                      JPanel         contentPane;
                      static JTextField textField_ID;
                     private JButton    ConnectButton;
                     static JTextField indexTextField;
                     static JTextField maxTextField;
      
      
            //Create the frame.
           public Display() 
           {
                
           
                super( "Display" ); //Setting Title to GUI
                
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close the Program on Exit
      
           JLabel lblId = new JLabel(" ID: "); 
                panelUser.add(lblId, "12, 4"); 
                
                textField_ID = new JTextField();  
                panelUser.add(textField_ID, "14, 4, 3, 1, fill, default"); 
                textField_ID.setColumns(10); 
      
      ConnectButton.setText("Connect to Database"); //Setting Text to ConnectButton
                ConnectButton.setEnabled(true); //Setting Enabled to True 
                ConnectButton.addActionListener(StaticMap.listener.get("CONNECT")); //Getting ActionListener From JavaClass StaticMap 
                navigatePanel.add(ConnectButton); // Adding ConnectButton to NavigatePanel
      
      }
      Edited by: 992563 on Mar 7, 2013 2:40 PM
        • 1. Re: Hash Mapping
          gimbal2
          Why on earth are you making everything static? Whenever you use that keyword you're probably doing it wrong, it is something you should only use by exception.

          Try designing your code such that you do not need them at all, I have the sneaky suspicion whatever problem you are having will magically go away.
          • 2. Re: Hash Mapping
            995566
            I had the Text Fields at Protected and private and still didn't make any change to it, i thought by changing them to Static it might Magical work.

            plus when i Take Static off the Text Fields,

            Its telling me to change it back to Static
            • 3. Re: Hash Mapping
              J2EE_Life
              Not only Textfields (and other variables) , take static away from methods also
              • 4. Re: Hash Mapping
                995566
                WHen i do that

                i get this

                Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
                     Cannot make a static reference to the non-static method ConnectButtonActionPerformed(ActionEvent) from the type StaticMap
                  
                       listener.put("CONNECT", new ActionListener() {
                
                          
                               @Override
                               
                               public  void actionPerformed(ActionEvent e) 
                               {
                                     ConnectButtonActionPerformed( e );
                               }
                            });
                 public  void ConnectButtonActionPerformed( ActionEvent evt )
                        {
                             
                                
                        
                                try
                                {
                                   results = Queries.getAllPeople();
                              
                                   
                                      
                                  numberOfEntries = results.size();
                                
                               
                                   if ( numberOfEntries != 0 )
                                   {
                                      currentEntryIndex = 0;
                                      currentEntry = results.get( currentEntryIndex );
                                      this.textField_ID.setText("" + currentEntry.getsID() );
                Edited by: 992563 on Mar 7, 2013 11:18 PM
                • 5. Re: Hash Mapping
                  J2EE_Life
                  I just saw the static block as below:
                  static 
                   
                       {
                            
                            doctorQueries = new DoctorQueries();
                            
                                          ................................................................
                  Where is the closing brace for this static block? You happen to define everything inside this static block, even the methods???
                  • 6. Re: Hash Mapping
                    Kayaman
                    992563 wrote:
                    WHen i do that

                    i get this

                    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
                         Cannot make a static reference to the non-static method ConnectButtonActionPerformed(ActionEvent) from the type StaticMap
                    If you had learned the basics of Java programming, you would know what's wrong.

                    I'm getting the feeling you're some $5/hour outsourcing worker, who doesn't actually have any idea what he's doing.
                    You need to read the tutorials and learn the language.
                    • 7. Re: Hash Mapping
                      995566
                      Keyaman,

                      I am new to Java coming from C / C++ and Visual Basic back ground..

                      And i wouldn't mind being some outsource working getting paid to work on peoples code
                      • 8. Re: Hash Mapping
                        995566
                        public class StaticMap 
                        {
                             
                                
                              static Person currentEntry;
                                static DoctorQueries doctorQueries;
                                static List<Person> results; 
                                 static int numberOfEntries = 0;
                              static int currentEntryIndex;
                               
                        private JTextField TextField_Id;
                        static 
                             {
                                  
                                  doctorQueries = new DoctorQueries();
                                  
                                    listener.put("CONNECT", new ActionListener() {
                        
                                       public  void actionPerformed(ActionEvent e) 
                                       {
                                             ConnectButtonActionPerformed( e );
                                       }
                                    });
                               
                        }
                        
                        
                        
                           public void ConnectButtonActionPerformed( ActionEvent evt )
                                {
                                     
                                        
                                
                                        try
                                        {
                                           results = doctorQueries.getAllPeople();
                                      
                                           
                                              
                                          numberOfEntries = results.size();
                                        
                                       
                                           if ( numberOfEntries != 0 )
                                           {
                                              currentEntryIndex = 0;
                                              currentEntry = results.get( currentEntryIndex );
                                              this.textField_ID.setText("" + currentEntry.getsID() );
                                                this.textField_FirstName.setText( currentEntry.getFirstName() );
                                                this.textField_LastName.setText( currentEntry.getLastName() );
                                                this.textField_Address.setText( currentEntry.getAddress() );
                                                this.textField_Number.setText( currentEntry.getPhoneNumber() );
                                              this.maxTextField.setText( "" + numberOfEntries );
                                             this.indexTextField.setText( "" + ( currentEntryIndex + 1 ) );
                                              this.nextButton.setEnabled( true );
                                             this.previousButton.setEnabled( true ); 
                                           
                                           } // end if 
                                         
                                        } // end try
                                        catch ( Exception e )
                                        {
                                           e.printStackTrace();
                                        } // end catch
                                       
                                     } // end method browseButtonActionPerformed
                                } // End Public Static Map
                               
                        So my ActionPerformed is outside the public static Map <String, ActionListener> listener = new HashMap <String, ActionListener> ();
                        • 9. Re: Hash Mapping
                          J2EE_Life
                          static 
                               {
                                    
                                    doctorQueries = new DoctorQueries();
                                    
                                      listener.put("CONNECT", new ActionListener() {
                           
                                         public  void actionPerformed(ActionEvent e) 
                                         {
                                               ConnectButtonActionPerformed( e );
                                         }
                                      });
                                 
                          }
                          Where is listener as used above. Do you need static block? Since static methods or static blocks can only refer to static variables , Do u need to define that static block and make everything static?
                          • 10. Re: Hash Mapping
                            Kayaman
                            992563 wrote:
                            I am new to Java coming from C / C++ and Visual Basic back ground..
                            Then go through the Java tutorials. Your background knowledge won't help here.
                            And i wouldn't mind being some outsource working getting paid to work on peoples code
                            I would mind if you were paid to work on Java code. You're an absolute beginner, so learn the basics.
                            • 11. Re: Hash Mapping
                              995566
                              My static map <"string" actionlistener> listener = new <string actionlistener>

                              Is defined at the top

                              I did a few debugging to the code

                              I place an system.out.print(result) so its getting
                              The information from the SQL database
                              But trying a null pointer where I trying to

                              Get the code to out put the data to the text fields inside the display java class
                              I also done

                              textfield_ID = new jTextfield() at the top of the static class
                              Etc.. For all the text fields
                              This didn't throw a null pointer but it didn't do anything either.
                              • 12. Re: Hash Mapping
                                995566
                                Thank u for your messages which aren't helpful but only trying to under mind some one looking for help..

                                I have gone through a lot of the basic to where I am now,

                                I have a program using JDBC, arraylists and abstract methods working along side each other

                                I hit a wall trying to get my controller to populate the text fields that are on my jpanel in my view model to populate..

                                I am trying to tidy every thing up by placing my database connection on a different class my SQL query on a different class and my action listeners on a different java class, every thing is working but the action listener class, if I can't get I to work I see no other choice but to place my action listeners back into the inner class. Where they were given me no problem..

                                Edited by: 992563 on Mar 9, 2013 2:42 AM
                                • 13. Re: Hash Mapping
                                  Kayaman
                                  992563 wrote:
                                  Thank u for your messages which aren't helpful but only trying to under mind some one looking for help..
                                  I'm trying to help you. You just don't understand it.
                                  I have gone through a lot of the basic to where I am now,
                                  I have a program using JDBC, arraylists and abstract methods working along side each other
                                  But you don't know what "static" means. It's a very basic thing in the language.

                                  The most helpful thing you could do would be to go through the language basics and learn what's the difference between static and non-static.
                                  • 14. Re: Hash Mapping
                                    baftos
                                    Besides what everybody said, part of the missing basics is how to read a stack trace.
                                    NullPointerException on the line
                                    textField_ID.setText(" " + currentEntry.getsID() ); 
                                    can only mean that textField_ID is null or currentEntry is null (something on the left of a dot).
                                    In the future, please copy and paste here the whole stack trace, when you ask for help with an exception.
                                    1 2 3 Previous Next