NullPointerException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    NullPointerException

    Code:
        private List<Klient> klienci;
         
           
            
     m_add_client.addActionListener( new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                        	
    try{
                        		
    
    klienci.add(new Klient((String)m_name.getText(),Double.valueOf(m_money.getText().trim()).doubleValue()));               		
                        	
    }catch(NumberFormatException exception){
    System.err.println("Nie podana suma pieniedzy");
    }  
    catch(NullPointerException exception){
    System.err.println("?");
    }
    }
    });
                    
     m_add.addActionListener( new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                        	
                        	try{
                        	Cennik c= Cennik.getInstance();
                        	Klient klient = new Klient((String)m_name.getText(),Double.valueOf(m_money.getText().trim()).doubleValue()); 
                        	if(klienci.contains(klient)){
                        		int index=klienci.indexOf(klient);
                        		klient=klienci.get(index);
                        	}
    
                        	String wybrany_kwiat= (String)kwiatyList.getSelectedItem();
                    	    System.out.print(wybrany_kwiat);
                        	int a=Integer.parseInt(m_amount.getText());
                        	table.addRow(wybrany_kwiat,a,c.getCenna(wybrany_kwiat));
                        	 
                        	if("Roza Czerwona".equals(wybrany_kwiat)){
                        	
                        	klient.dodajDoWozka(new RozaCzerwona(),a);
                        	
                        	}else if("Roza Biala".equals(wybrany_kwiat)){
                            	
                          klient.dodajDoWozka(new RozaBiala(),a);
                            	
                           }else if("Roza Zolta".equals(wybrany_kwiat)){
                            klient.dodajDoWozka(new RozaZolta(),a);
                                	
                           }else if("Roza Czarna".equals(wybrany_kwiat)){
                          	
                        	klient.dodajDoWozka(new RozaCzarna(),a);
                                   	
                          }else if("Tulipan Zolty".equals(wybrany_kwiat)){
                          	
                        	klient.dodajDoWozka(new TulipanZolty(),a);
                                   	
                          }else if("Tulipan Czerwony".equals(wybrany_kwiat)){
                          	
                        	klient.dodajDoWozka(new TulipanCzerwony(),a);
                                   	
                          }else if("Tulipan Bialy".equals(wybrany_kwiat)){
                          	
                        	klient.dodajDoWozka(new TulipanBialy(),a);
                                   	
                          }else if("Lilia Biala".equals(wybrany_kwiat)){
                          	
                        	klient.dodajDoWozka(new LiliaBiala(),a);
                                   	
                          }else if("Lilia Zolta".equals(wybrany_kwiat)){
                          	
                        	klient.dodajDoWozka(new LiliaZolta(),a);
                                   	
                          }else if("Lilia Rozowa".equals(wybrany_kwiat)){
                          	
                        	klient.dodajDoWozka(new LiliaRozowa(),a);
                                   	
                          }
                    
                        	}catch(NullPointerException exception){
                        		
                        		 System.err.println("Funkcja c.getCenna(wybrany_kwiat)");
                        	}
                        	label_koszyk.setText("Do zaplaty:" + kwiaciarnia.doZaplaty());	 
                        	
                        
                        }
                      });
    somewhere here i get nullpointerexce ption but where ?
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Does it give you a line number of the exectption?

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      no it doesnt

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #4
        the nullpointerexce ption is in a line with klient.dodajDoW ozka(new RozaCzerwona(), a);

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          Code:
          public class Roza extends Kwiat{
          
              public Roza(){
              }
              protected String get_nazwa(){return nazwa;}
              protected  double get_cena() {return cena;}
              protected  void set_nazwa(String n){nazwa=n;}
              protected  void set_cena(double c){cena=c;}
          }
          public class RozaCzerwona extends Roza{
          String nazwa="Roza Czerwona";
          
          public RozaCzerwona(){}
          }
          why RozaCzerwona r= new RozaCzerwona(); returns null

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            This is the show stopper:
            Code:
            catch(NullPointerException exception){
            System.err.println("?");
            }
            The exception could've given you all the information you need but all you do
            is print a single question mark instead. At least do this in the body of your
            exception handler and see the entire stack trace:
            Code:
            exception.printStackTrace();
            Never ever hide information you might need.

            kind regards,

            Jos

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #7
              i know that nullpointerexce ption is caused by klient.dodajDoW ozka(new RozaCzerwona(), a); cos new RozaCzerwona() returns null

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                The new operator can never return null. The cause of your NullPointerExce ption
                is somewhere else.

                kind regards,

                Jos

                Comment

                • oll3i
                  Contributor
                  • Mar 2007
                  • 679

                  #9
                  i checked it i commented out everything
                  and this line klient.dodajDoW ozka(new RozaCzerwona(), a);caused nullpointerexce ption

                  then i did
                  RozaCzerwona r= new RozaCzerwona();
                  System.out.prin tln(r.get_nazwa ());<<< this prints null

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by oll3i
                    i checked it i commented out everything
                    and this line klient.dodajDoW ozka(new RozaCzerwona(), a);caused nullpointerexce ption

                    then i did
                    RozaCzerwona r= new RozaCzerwona();
                    System.out.prin tln(r.get_nazwa ());<<< this prints null
                    That only shows that your 'get_nazwa()' method returned null.

                    kind regards,

                    Jos

                    Comment

                    • oll3i
                      Contributor
                      • Mar 2007
                      • 679

                      #11
                      yes but why

                      this are the classes

                      Code:
                      public class RozaCzerwona extends Roza{
                      String nazwa="Roza Czerwona";
                      
                      public RozaCzerwona(){}
                      }
                      
                      public class Roza extends Kwiat{
                      
                          public Roza(){
                          }
                          protected  String get_nazwa(){return nazwa;}
                          protected  double get_cena() {return cena;}
                          protected  void set_nazwa(String n){nazwa=n;}
                          protected  void set_cena(double c){cena=c;}
                      }
                      
                      public abstract class Kwiat {
                      	String nazwa;
                      	double cena;
                      	protected abstract String get_nazwa();
                      	protected abstract double get_cena();
                      	protected abstract void set_nazwa(String n);
                      	protected abstract void set_cena(double c);
                      }

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by oll3i
                        yes but why

                        this are the classes

                        Code:
                        public class RozaCzerwona extends Roza{
                        String nazwa="Roza Czerwona";
                        
                        public RozaCzerwona(){}
                        }
                        
                        public class Roza extends Kwiat{
                        
                            public Roza(){
                            }
                            protected  String get_nazwa(){return nazwa;}
                            protected  double get_cena() {return cena;}
                            protected  void set_nazwa(String n){nazwa=n;}
                            protected  void set_cena(double c){cena=c;}
                        }
                        
                        public abstract class Kwiat {
                        	String nazwa;
                        	double cena;
                        	protected abstract String get_nazwa();
                        	protected abstract double get_cena();
                        	protected abstract void set_nazwa(String n);
                        	protected abstract void set_cena(double c);
                        }
                        You have *two* Strings "nazwa": one in the abstract base class Kwiat and
                        another one in the derived class RozaCzerwona. You initialize the latter
                        but the String in the base class still equals null and that is the one returned
                        by your get_nazwa() method.

                        Change your constructor in that derived class to:
                        Code:
                        public RozaCzerwona(){
                           set_nazwa("Roza Czerwona");
                        }
                        ... and remove that String nazwa in the derived class.

                        kind regards,

                        Jos

                        Comment

                        • oll3i
                          Contributor
                          • Mar 2007
                          • 679

                          #13
                          that fixed the problem thank u very much :)

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by oll3i
                            that fixed the problem thank u very much :)
                            You're welcome of course; please reread my reply #6 again: you effectively
                            had hidden the fact that your variable was still null; your exception could've
                            told you that in its stack trace, but you didn't allow it to tell you so, so Eclipse
                            assumes that you've handled the exception and doesn't panic either.

                            Always let those exceptions tell what they want to tell you during the
                            development phase of your program. Only when you know what do handle
                            and what not you can take those stacktraces out of your program.

                            kind regards,

                            Jos

                            Comment

                            Working...