473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

NullPointerException

oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1.  
  2.     private List<Klient> klienci;
  3.  
  4.  
  5.  
  6.  m_add_client.addActionListener( new ActionListener() {
  7.                     public void actionPerformed(ActionEvent e) {
  8.  
  9. try{
  10.  
  11.  
  12. klienci.add(new Klient((String)m_name.getText(),Double.valueOf(m_money.getText().trim()).doubleValue()));                       
  13.  
  14. }catch(NumberFormatException exception){
  15. System.err.println("Nie podana suma pieniedzy");
  16. }  
  17. catch(NullPointerException exception){
  18. System.err.println("?");
  19. }
  20. }
  21. });
  22.  
  23.  m_add.addActionListener( new ActionListener() {
  24.                     public void actionPerformed(ActionEvent e) {
  25.  
  26.                         try{
  27.                         Cennik c= Cennik.getInstance();
  28.                         Klient klient = new Klient((String)m_name.getText(),Double.valueOf(m_money.getText().trim()).doubleValue()); 
  29.                         if(klienci.contains(klient)){
  30.                             int index=klienci.indexOf(klient);
  31.                             klient=klienci.get(index);
  32.                         }
  33.  
  34.                         String wybrany_kwiat= (String)kwiatyList.getSelectedItem();
  35.                         System.out.print(wybrany_kwiat);
  36.                         int a=Integer.parseInt(m_amount.getText());
  37.                         table.addRow(wybrany_kwiat,a,c.getCenna(wybrany_kwiat));
  38.  
  39.                         if("Roza Czerwona".equals(wybrany_kwiat)){
  40.  
  41.                         klient.dodajDoWozka(new RozaCzerwona(),a);
  42.  
  43.                         }else if("Roza Biala".equals(wybrany_kwiat)){
  44.  
  45.                       klient.dodajDoWozka(new RozaBiala(),a);
  46.  
  47.                        }else if("Roza Zolta".equals(wybrany_kwiat)){
  48.                         klient.dodajDoWozka(new RozaZolta(),a);
  49.  
  50.                        }else if("Roza Czarna".equals(wybrany_kwiat)){
  51.  
  52.                         klient.dodajDoWozka(new RozaCzarna(),a);
  53.  
  54.                       }else if("Tulipan Zolty".equals(wybrany_kwiat)){
  55.  
  56.                         klient.dodajDoWozka(new TulipanZolty(),a);
  57.  
  58.                       }else if("Tulipan Czerwony".equals(wybrany_kwiat)){
  59.  
  60.                         klient.dodajDoWozka(new TulipanCzerwony(),a);
  61.  
  62.                       }else if("Tulipan Bialy".equals(wybrany_kwiat)){
  63.  
  64.                         klient.dodajDoWozka(new TulipanBialy(),a);
  65.  
  66.                       }else if("Lilia Biala".equals(wybrany_kwiat)){
  67.  
  68.                         klient.dodajDoWozka(new LiliaBiala(),a);
  69.  
  70.                       }else if("Lilia Zolta".equals(wybrany_kwiat)){
  71.  
  72.                         klient.dodajDoWozka(new LiliaZolta(),a);
  73.  
  74.                       }else if("Lilia Rozowa".equals(wybrany_kwiat)){
  75.  
  76.                         klient.dodajDoWozka(new LiliaRozowa(),a);
  77.  
  78.                       }
  79.  
  80.                         }catch(NullPointerException exception){
  81.  
  82.                              System.err.println("Funkcja c.getCenna(wybrany_kwiat)");
  83.                         }
  84.                         label_koszyk.setText("Do zaplaty:" + kwiaciarnia.doZaplaty());     
  85.  
  86.  
  87.                     }
  88.                   });   
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
somewhere here i get nullpointerexception but where ?
Apr 6 '07 #1
13 1939
RedSon
5,000 Expert 4TB
Does it give you a line number of the exectption?
Apr 6 '07 #2
oll3i
679 512MB
no it doesnt
Apr 6 '07 #3
oll3i
679 512MB
the nullpointerexception is in a line with klient.dodajDoWozka(new RozaCzerwona(),a);
Apr 8 '07 #4
oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1. public class Roza extends Kwiat{
  2.  
  3.     public Roza(){
  4.     }
  5.     protected String get_nazwa(){return nazwa;}
  6.     protected  double get_cena() {return cena;}
  7.     protected  void set_nazwa(String n){nazwa=n;}
  8.     protected  void set_cena(double c){cena=c;}
  9. }
  10. public class RozaCzerwona extends Roza{
  11. String nazwa="Roza Czerwona";
  12.  
  13. public RozaCzerwona(){}
  14. }
  15.  
why RozaCzerwona r= new RozaCzerwona(); returns null
Apr 8 '07 #5
JosAH
11,448 Expert 8TB
This is the show stopper:
Expand|Select|Wrap|Line Numbers
  1. catch(NullPointerException exception){
  2. System.err.println("?");
  3. }
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:
Expand|Select|Wrap|Line Numbers
  1. exception.printStackTrace();
Never ever hide information you might need.

kind regards,

Jos
Apr 8 '07 #6
oll3i
679 512MB
i know that nullpointerexception is caused by klient.dodajDoWozka(new RozaCzerwona(),a); cos new RozaCzerwona() returns null
Apr 8 '07 #7
JosAH
11,448 Expert 8TB
The new operator can never return null. The cause of your NullPointerException
is somewhere else.

kind regards,

Jos
Apr 9 '07 #8
oll3i
679 512MB
i checked it i commented out everything
and this line klient.dodajDoWozka(new RozaCzerwona(),a);caused nullpointerexception

then i did
RozaCzerwona r= new RozaCzerwona();
System.out.println(r.get_nazwa());<<< this prints null
Apr 9 '07 #9
JosAH
11,448 Expert 8TB
i checked it i commented out everything
and this line klient.dodajDoWozka(new RozaCzerwona(),a);caused nullpointerexception

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

kind regards,

Jos
Apr 9 '07 #10
oll3i
679 512MB
yes but why

this are the classes

Expand|Select|Wrap|Line Numbers
  1. public class RozaCzerwona extends Roza{
  2. String nazwa="Roza Czerwona";
  3.  
  4. public RozaCzerwona(){}
  5. }
  6.  
  7. public class Roza extends Kwiat{
  8.  
  9.     public Roza(){
  10.     }
  11.     protected  String get_nazwa(){return nazwa;}
  12.     protected  double get_cena() {return cena;}
  13.     protected  void set_nazwa(String n){nazwa=n;}
  14.     protected  void set_cena(double c){cena=c;}
  15. }
  16.  
  17. public abstract class Kwiat {
  18.     String nazwa;
  19.     double cena;
  20.     protected abstract String get_nazwa();
  21.     protected abstract double get_cena();
  22.     protected abstract void set_nazwa(String n);
  23.     protected abstract void set_cena(double c);
  24. }
  25.  
Apr 9 '07 #11
JosAH
11,448 Expert 8TB
yes but why

this are the classes

Expand|Select|Wrap|Line Numbers
  1. public class RozaCzerwona extends Roza{
  2. String nazwa="Roza Czerwona";
  3.  
  4. public RozaCzerwona(){}
  5. }
  6.  
  7. public class Roza extends Kwiat{
  8.  
  9.     public Roza(){
  10.     }
  11.     protected  String get_nazwa(){return nazwa;}
  12.     protected  double get_cena() {return cena;}
  13.     protected  void set_nazwa(String n){nazwa=n;}
  14.     protected  void set_cena(double c){cena=c;}
  15. }
  16.  
  17. public abstract class Kwiat {
  18.     String nazwa;
  19.     double cena;
  20.     protected abstract String get_nazwa();
  21.     protected abstract double get_cena();
  22.     protected abstract void set_nazwa(String n);
  23.     protected abstract void set_cena(double c);
  24. }
  25.  
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:
Expand|Select|Wrap|Line Numbers
  1. public RozaCzerwona(){
  2.    set_nazwa("Roza Czerwona");
  3. }
... and remove that String nazwa in the derived class.

kind regards,

Jos
Apr 9 '07 #12
oll3i
679 512MB
that fixed the problem thank u very much :)
Apr 9 '07 #13
JosAH
11,448 Expert 8TB
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
Apr 9 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: K S Aldebaraan | last post by:
I'm trying to submit a form with an action of a servlet, and a view equal to the same jsp page. I'm not sure what I'm doing wrong, but keep getting a NullPointerException on the second line of...
4
by: gabryh | last post by:
Hi, The following code throws me NullPointerException. ..... public static boolean isEmpty(String value) { return ((value == null) || (value.trim().equals(""))); }
0
by: Old-timer | last post by:
Not sure where else to post this. I'm sure I'm doing something wrong, but I wouldn't think a simple app would give me so much trouble. I've got a small test java class that I'm trying to have...
2
by: Smith | last post by:
The program compiled successfully, but it gives the following error on runtime.... java.lang.NullPointerException at FrogManiaApp.paint(FrogManiaApp.java:102) at...
1
by: ketand1 | last post by:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.sql.*; import java.lang.*; class DbAwt extends Frame implements ActionListener { private...
2
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
3
by: chris123456789 | last post by:
Hi, when I run my code I get a NullPointerException:null. Here is the part of the code where the error occurs: import java.util.*; import java.io.*; public class Decrypt { ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.