473,401 Members | 2,127 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,401 software developers and data experts.

NullPointerException

oll3i
679 512MB
nevermind the previous prob i posted

i m getting further and i get a new exception
in readFile method pleae help me with that one
thank u a lot in advance

java.lang.NullPointerException
at konta_bankowe.Interfejs.readFile(Interfejs.java)
at konta_bankowe.Interfejs.wyswietlHistorieRachunku(I nterfejs.java)
at konta_bankowe.Interfejs.access$0(Interfejs.java)
at konta_bankowe.Interfejs$1.actionPerformed(Interfej s.java)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)




and the code is
Expand|Select|Wrap|Line Numbers
  1. package konta_bankowe;
  2.  
  3.  
  4.  
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.beans.*;
  8. import java.io.BufferedReader;
  9. import java.io.DataInputStream;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.io.InputStreamReader;
  14. import java.text.NumberFormat;
  15.  
  16. import javax.swing.*;
  17.  
  18. import static java.awt.Color.*;
  19. import static java.lang.Thread.sleep;
  20. import static java.lang.System.out;
  21.  
  22. public class Interfejs extends JFrame {
  23.  
  24.   JTextArea report = new JTextArea(25, 40);
  25.  
  26.   public Interfejs() {
  27.       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  28.  
  29.       setTitle("Program kont bankowych(Katarzyna Olbromska).");
  30.       JPanel panel = new JPanel();
  31.       JLabel label = new JLabel("Podaj Number konta :");
  32.       final JTextField numer_konta_field = new JTextField();
  33.  
  34.       numer_konta_field.setColumns(10);
  35.       panel.add(label);
  36.       panel.add(numer_konta_field);
  37.       JButton button = new JButton();
  38.       button.setLabel("Szukaj");
  39.       button.addActionListener( new ActionListener() {
  40.           public void actionPerformed(ActionEvent e) {
  41.  
  42.                 wyswietlHistorieRachunku(numer_konta_field.getText());
  43.  
  44.  
  45.           }
  46.         });
  47.       panel.add(button);
  48.       getContentPane().add(panel, BorderLayout.SOUTH);
  49.       pack();
  50.  
  51.  
  52.  
  53.  
  54.     report.setBorder(BorderFactory.createTitledBorder("Raport z kont"));
  55.     add(new JScrollPane(report));
  56.  
  57.     JPanel p = new JPanel();
  58.  
  59.     final JLabel lab = new JLabel("Konta");
  60.     lab.setOpaque(true);
  61.     p.add(lab);
  62.  
  63.     final JButton b1 = new JButton("Złote");
  64.     setColor(YELLOW, b1);
  65.     b1.addActionListener( new ActionListener() {
  66.       public void actionPerformed(ActionEvent e) {
  67.  
  68.       }
  69.     });
  70.     p.add(b1);
  71.  
  72.     final JButton b2 = new JButton("Srebrne");
  73.     Color c1 = new Color(213, 213, 213);
  74.     //g.setColor(c);
  75.     setColor(c1, b2);
  76.     b2.addActionListener( new ActionListener() {
  77.       public void actionPerformed(ActionEvent e) {
  78.  
  79.       }
  80.     });
  81.     p.add(b2);
  82.  
  83.     final JButton b3 = new JButton("Brązowe");
  84.     Color c2 = new Color(144, 124, 72);
  85.     //g.setColor(c);
  86.     setColor(c2, b3);
  87.     b3.addActionListener( new ActionListener() {
  88.       public void actionPerformed(ActionEvent e) {
  89.  
  90.       }
  91.     });
  92.     p.add(b3);
  93.  
  94.     final JButton b4 = new JButton("Normalne");
  95.     setColor(WHITE, b4);
  96.     b3.addActionListener( new ActionListener() {
  97.       public void actionPerformed(ActionEvent e) {
  98.  
  99.       }
  100.     });
  101.     p.add(b4);
  102.     add(p, "North"); 
  103.  
  104.     pack();
  105.     setVisible(true);
  106.   }
  107.  
  108.   private void setColor(final Color color, final Component ... comps) {
  109.  
  110.         for (Component c : comps) c.setBackground(color);
  111.  
  112.       }
  113.  
  114.   private void wyswietlHistorieRachunku(String numer_konta) {
  115.         report.setText(" "); 
  116.         //report.append("Numer konta: " +numer_konta +'\n');
  117.         report.append("Historia Rachunku: " +'\n');
  118.         readFile(numer_konta);
  119.       }
  120.  
  121.  
  122.  
  123. private void readFile(String numer_konta){
  124.  
  125.     try{
  126.         // Open the file that is the first 
  127.         // command line parameter
  128.         FileInputStream fstream = new FileInputStream("konta//"+numer_konta+".txt");
  129.         // Get the object of DataInputStream
  130.         DataInputStream in = new DataInputStream(fstream);
  131.         BufferedReader br = new BufferedReader(new InputStreamReader(in));
  132.         String strLine;
  133.         String[] historia  = null;
  134.  
  135.  
  136.       int n=0;
  137.       while((strLine = br.readLine()) != null)   {   
  138.       historia[n]=strLine; 
  139.       System.out.println("historia[n]="+historia[n]+"strLine="+strLine);
  140.       n++;
  141.       }
  142.  
  143.  
  144.       for (String h : historia)
  145.           report.append(h +'\n');
  146.  
  147.  
  148.  
  149.         in.close();
  150.       }catch (FileNotFoundException e) {
  151.             System.err.println("Nie ma takiego konta(File not found!)");
  152.       }catch (IOException e) {
  153.               System.err.println(e);
  154.       }catch (Exception ex)  {
  155.       ex.printStackTrace();  }    
  156.  
  157.  
  158.  
  159.  
  160. }      
  161. }
  162.  
  163.  
  164.  
  165. package konta_bankowe;
  166.  
  167.  
  168. import java.io.*;
  169. import java.util.HashMap;
  170. import java.util.Map;
  171. import java.applet.Applet;
  172. import java.awt.*;
  173. import java.awt.event.*;
  174.  
  175. public class Konto {
  176.  
  177.  
  178.     /**
  179.      * @param args
  180.      */
  181.     protected double sr_dost;
  182.     private String NumerKonta;
  183.  
  184.  
  185.  
  186.             public Konto()
  187.              { new Interfejs();}
  188.  
  189.             public void depozyt( double amount )
  190.             {
  191.                 sr_dost += amount;
  192.             }
  193.  
  194.                 public double wyplata( double amount )
  195.             {
  196.                         // See if amount can be withdrawn
  197.                 if (sr_dost >= amount)
  198.                 {
  199.                     sr_dost -= amount;
  200.                                 return amount;
  201.                 }
  202.                 else
  203.                         // Withdrawal not allowed
  204.                                 return 0.0;
  205.             }
  206.  
  207.                 public double get_sr_dost()
  208.             {
  209.                  return sr_dost;
  210.             }     
  211.  
  212.  
  213.  
  214.  
  215. public static void main(String[] args)throws IOException {
  216. Konto konto = new Konto();
  217.  
  218. }
  219. }
Mar 11 '07 #1
17 1762
sicarie
4,677 Expert Mod 4TB
Please use code tags ( [ code] and [ /code] without the spaces) - it helps the readability of your posts.

Where is the file you are trying to open? Is it in the same directory, or one below it? It looks like you're using a relative pathname - try using the absolute (which will depend on the machine).
Expand|Select|Wrap|Line Numbers
  1. Windows:
  2. C:\\Windows\\....
  3.  
I'm not sure if that's exactly it, you might have to play around with the slashes - you can use the forward in Windows, but I don't know if you have to backwhack them (adding an extra one so that the compiler reads it as a single slash...) or not.

Expand|Select|Wrap|Line Numbers
  1. Linux:
  2. /home/user/docs/yourfile
  3.  
Once again, you might need to backwhack that...

Try those, see if you are actually pointing to the correct location.
Mar 11 '07 #2
r035198x
13,262 8TB
nevermind the previous prob i posted

i m getting further and i get a new exception
in readFile method pleae help me with that one
thank u a lot in advance

java.lang.NullPointerException
at konta_bankowe.Interfejs.readFile(Interfejs.java)
at konta_bankowe.Interfejs.wyswietlHistorieRachunku(I nterfejs.java)
at konta_bankowe.Interfejs.access$0(Interfejs.java)
at konta_bankowe.Interfejs$1.actionPerformed(Interfej s.java)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)




and the code is

package konta_bankowe;



import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.NumberFormat;

import javax.swing.*;

import static java.awt.Color.*;
import static java.lang.Thread.sleep;
import static java.lang.System.out;

public class Interfejs extends JFrame {

JTextArea report = new JTextArea(25, 40);

public Interfejs() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setTitle("Program kont bankowych(Katarzyna Olbromska).");
JPanel panel = new JPanel();
JLabel label = new JLabel("Podaj Number konta :");
final JTextField numer_konta_field = new JTextField();

numer_konta_field.setColumns(10);
panel.add(label);
panel.add(numer_konta_field);
JButton button = new JButton();
button.setLabel("Szukaj");
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {

wyswietlHistorieRachunku(numer_konta_field.getText ());


}
});
panel.add(button);
getContentPane().add(panel, BorderLayout.SOUTH);
pack();




report.setBorder(BorderFactory.createTitledBorder( "Raport z kont"));
add(new JScrollPane(report));

JPanel p = new JPanel();

final JLabel lab = new JLabel("Konta");
lab.setOpaque(true);
p.add(lab);

final JButton b1 = new JButton("Złote");
setColor(YELLOW, b1);
b1.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
p.add(b1);

final JButton b2 = new JButton("Srebrne");
Color c1 = new Color(213, 213, 213);
//g.setColor(c);
setColor(c1, b2);
b2.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
p.add(b2);

final JButton b3 = new JButton("Brązowe");
Color c2 = new Color(144, 124, 72);
//g.setColor(c);
setColor(c2, b3);
b3.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
p.add(b3);

final JButton b4 = new JButton("Normalne");
setColor(WHITE, b4);
b3.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
p.add(b4);
add(p, "North");

pack();
setVisible(true);
}

private void setColor(final Color color, final Component ... comps) {

for (Component c : comps) c.setBackground(color);

}

private void wyswietlHistorieRachunku(String numer_konta) {
report.setText(" ");
//report.append("Numer konta: " +numer_konta +'\n');
report.append("Historia Rachunku: " +'\n');
readFile(numer_konta);
}



private void readFile(String numer_konta){

try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("konta//"+numer_konta+".txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String[] historia = null;


int n=0;
while((strLine = br.readLine()) != null) {
historia[n]=strLine;
System.out.println("historia[n]="+historia[n]+"strLine="+strLine);
n++;
}


for (String h : historia)
report.append(h +'\n');



in.close();
}catch (FileNotFoundException e) {
System.err.println("Nie ma takiego konta(File not found!)");
}catch (IOException e) {
System.err.println(e);
}catch (Exception ex) {
ex.printStackTrace(); }




}
}



package konta_bankowe;


import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Konto {


/**
* @param args
*/
protected double sr_dost;
private String NumerKonta;



public Konto()
{ new Interfejs();}

public void depozyt( double amount )
{
sr_dost += amount;
}

public double wyplata( double amount )
{
// See if amount can be withdrawn
if (sr_dost >= amount)
{
sr_dost -= amount;
return amount;
}
else
// Withdrawal not allowed
return 0.0;
}

public double get_sr_dost()
{
return sr_dost;
}




public static void main(String[] args)throws IOException {
Konto konto = new Konto();

}
}
1.) Use code tags when posting code.
2.)Do not use DataInputStream for reading a .txt file
Use FileReader fr = new FileReader("fileName.txt");
BufferedReader br = new BufferedReader(fr);

3.) Here is the reason why you are getting null pointer exception

Expand|Select|Wrap|Line Numbers
  1. String[] historia = null;
  2.  
You initialized this array to null.

Expand|Select|Wrap|Line Numbers
  1. int n=0;
  2. while((strLine = br.readLine()) != null) { 
  3.  
There is no problem here ....

...but
Expand|Select|Wrap|Line Numbers
  1. historia[n]=strLine;
You try to assign something into an array which you have initialized to null. This will give the NullPointerException.
Either initialize the array first
Expand|Select|Wrap|Line Numbers
  1. String[] historia = new String[someInteger];
  2.  
if you know how many lines there are to read or use an ArrayList to store the lines if you do not know how many lines are going to be read.
Mar 12 '07 #3
oll3i
679 512MB
thank you for ur reply
i did what u said me to do and now i'm getting


Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
in cannot be resolved

at konta_bankowe.Interfejs.readFile(Interfejs.java)
at konta_bankowe.Interfejs.wyswietlHistorieRachunku(I nterfejs.java)
at konta_bankowe.Interfejs.access$0(Interfejs.java)
at konta_bankowe.Interfejs$1.actionPerformed(Interfej s.java)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Mar 12 '07 #4
r035198x
13,262 8TB
thank you for ur reply
i did what u said me to do and now i'm getting


Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
in cannot be resolved

at konta_bankowe.Interfejs.readFile(Interfejs.java)
at konta_bankowe.Interfejs.wyswietlHistorieRachunku(I nterfejs.java)
at konta_bankowe.Interfejs.access$0(Interfejs.java)
at konta_bankowe.Interfejs$1.actionPerformed(Interfej s.java)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Post the code that you have now using code tags.
Mar 12 '07 #5
oll3i
679 512MB
i fixed it thank u
Mar 12 '07 #6
r035198x
13,262 8TB
i fixed it thank u
Good for you.

Did you use the FileReader for it then?
Mar 12 '07 #7
oll3i
679 512MB
but i get another NullPointerException
in the method below also it doesnt find the rodzaj_konta in string and it goes 2 times through the for loop why

private Map<Integer,String> readFiles(String[] file_names,String rodzaj_konta) {
Map<Integer,String> map = new HashMap<Integer,String>();

String wczytane_dane=null;
for(int i = 0;i<file_names.length;i++){

boolean jest_rodzaj_konta=false;
int j=0;
try{
BufferedReader in = new BufferedReader(new FileReader("konta//"+file_names[i]));

String line = in.readLine();


while (line != null) {
line = in.readLine();
wczytane_dane.concat(line);

}


if (wczytane_dane.indexOf(rodzaj_konta) > 0) {jest_rodzaj_konta =true;}

if(jest_rodzaj_konta){
String numer_konta=null;
String[] dane=wczytane_dane.split(":");
numer_konta=dane[1];


map.put(j,numer_konta);
j++;
}
in.close();
}catch (FileNotFoundException e) {
System.err.println("File not found!)");
}catch (IOException e) {
System.err.println(e);
}catch (Exception ex) {
ex.printStackTrace(); }
wczytane_dane=null;
}




return map;
}
Mar 12 '07 #8
oll3i
679 512MB
it goes through all the files in the directory 2 times

yes i used BufferedReader in = new BufferedReader(new FileReader("konta//"+file_names[i]));
Mar 12 '07 #9
r035198x
13,262 8TB
but i get another NullPointerException
in the method below also it doesnt find the rodzaj_konta in string and it goes 2 times through the for loop why

private Map<Integer,String> readFiles(String[] file_names,String rodzaj_konta) {
Map<Integer,String> map = new HashMap<Integer,String>();

String wczytane_dane=null;
for(int i = 0;i<file_names.length;i++){

boolean jest_rodzaj_konta=false;
int j=0;
try{
BufferedReader in = new BufferedReader(new FileReader("konta//"+file_names[i]));

String line = in.readLine();


while (line != null) {
line = in.readLine();
wczytane_dane.concat(line);

}


if (wczytane_dane.indexOf(rodzaj_konta) > 0) {jest_rodzaj_konta =true;}

if(jest_rodzaj_konta){
String numer_konta=null;
String[] dane=wczytane_dane.split(":");
numer_konta=dane[1];


map.put(j,numer_konta);
j++;
}
in.close();
}catch (FileNotFoundException e) {
System.err.println("File not found!)");
}catch (IOException e) {
System.err.println(e);
}catch (Exception ex) {
ex.printStackTrace(); }
wczytane_dane=null;
}




return map;
}
There are two possible sources of null pointer this time

String line = in.readLine(); //reads a line
while (line != null) { //tests if it's null that's fine
line = in.readLine(); // reads the next line again! the next line can be null and is null after the last line is read. so
wczytane_dane.concat(line); will give a null pointer.

the second source is not definite and may be the arguments you are supplying.
Change the reading to
Expand|Select|Wrap|Line Numbers
  1.  String line = in.readLine(); 
  2.       while (line != null) {
  3.            wczytane_dane.concat(line);
  4.             line = in.readLine();
  5.       }
  6.  
Mar 12 '07 #10
oll3i
679 512MB
i moved the line wczytane_dane.concat(line);
before
line = in.readLine();

but i still get NullPointerException
Mar 12 '07 #11
oll3i
679 512MB
it doesnt even seem to read in the line from file cos i did system.out.println to test it and it displays nothing

here in this while loop

while (line != null) {
wczytane_dane.concat(line);
line = in.readLine();
System.out.println("wczytane_dane"+wczytane_dane);
}
Mar 12 '07 #12
oll3i
679 512MB
i forgot the code tags sorry
Mar 12 '07 #13
r035198x
13,262 8TB
it doesnt even seem to read in the line from file cos i did system.out.println to test it and it displays nothing

here in this while loop

while (line != null) {
wczytane_dane.concat(line);
line = in.readLine();
System.out.println("wczytane_dane"+wczytane_dane);
}
What is wczytane_dane supposed to be?
Can you post the code for the whole class and a brief explanation of what the code is supposed to do.
Mar 12 '07 #14
oll3i
679 512MB
in readFiles i wannna go through the files array passed (this is String[] file_names argument )
then find a word (which is String rodzaj_konta argument)
in a string wczytane_dane(into this variable i read in the content of a file line by line cos i wanna look for a word in it)
then i want to return a map if a word searched for is found (that is if rodzaj_konta is found)
rodzaj_konta(it's in polish but it means account type and it can be "silver" , "brown" "normal" or "golden") so rodzaj_konta variable can have values"silver" , "brown" "normal" or "golden") if rodzaj_konta is found in wczytane_dane then boolean jest_rodzaj_konta is to have value true if it is true then i want to add the key and value to map <Intiger,String> key wd be eg 0 and the value wd be the account number got from the file cos i look in wczytane_dane for accout number also

hope u will understand what i wanna do ;]



Expand|Select|Wrap|Line Numbers
  1. package konta_bankowe;
  2.  
  3.  
  4.  
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.beans.*;
  8. import java.io.BufferedReader;
  9. import java.io.DataInputStream;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.FileReader;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16. import java.text.NumberFormat;
  17. import java.util.HashMap;
  18. import java.util.HashSet;
  19. import java.util.Map;
  20. import java.util.Set;
  21.  
  22. import javax.swing.*;
  23.  
  24. import static java.awt.Color.*;
  25. import static java.lang.Thread.sleep;
  26. import static java.lang.System.out;
  27.  
  28. public class Interfejs extends JFrame {
  29.  
  30.   JTextArea report = new JTextArea(25, 40);
  31.  
  32.   public Interfejs() {
  33.       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  34.  
  35.       setTitle("Program kont bankowych(Katarzyna Olbromska).");
  36.       JPanel panel = new JPanel();
  37.       JLabel label = new JLabel("Podaj Number konta :");
  38.       final JTextField numer_konta_field = new JTextField();
  39.  
  40.       numer_konta_field.setColumns(10);
  41.       panel.add(label);
  42.       panel.add(numer_konta_field);
  43.       JButton button = new JButton();
  44.       button.setLabel("Szukaj");
  45.       button.addActionListener( new ActionListener() {
  46.           public void actionPerformed(ActionEvent e) {
  47.  
  48.                 wyswietlHistorieRachunku(numer_konta_field.getText());
  49.  
  50.  
  51.           }
  52.         });
  53.       panel.add(button);
  54.       getContentPane().add(panel, BorderLayout.SOUTH);
  55.       pack();
  56.  
  57.  
  58.  
  59.  
  60.     report.setBorder(BorderFactory.createTitledBorder("Raport z kont"));
  61.     add(new JScrollPane(report));
  62.  
  63.     JPanel p = new JPanel();
  64.  
  65.     final JLabel lab = new JLabel("Konta");
  66.     lab.setOpaque(true);
  67.     p.add(lab);
  68.  
  69.     final JButton b1 = new JButton("Złote");
  70.     setColor(YELLOW, b1);
  71.     b1.addActionListener( new ActionListener() {
  72.       public void actionPerformed(ActionEvent e) {
  73.           wyswietlHistorieRachunkow("zlote");
  74.       }
  75.     });
  76.     p.add(b1);
  77.  
  78.     final JButton b2 = new JButton("Srebrne");
  79.     Color c1 = new Color(213, 213, 213);
  80.     //g.setColor(c);
  81.     setColor(c1, b2);
  82.     b2.addActionListener( new ActionListener() {
  83.       public void actionPerformed(ActionEvent e) {
  84.           wyswietlHistorieRachunkow("srebrne");
  85.       }
  86.     });
  87.     p.add(b2);
  88.  
  89.     final JButton b3 = new JButton("Brązowe");
  90.     Color c2 = new Color(144, 124, 72);
  91.     //g.setColor(c);
  92.     setColor(c2, b3);
  93.     b3.addActionListener( new ActionListener() {
  94.       public void actionPerformed(ActionEvent e) {
  95.           wyswietlHistorieRachunkow("brazowe");
  96.       }
  97.     });
  98.     p.add(b3);
  99.  
  100.     final JButton b4 = new JButton("Normalne");
  101.     setColor(WHITE, b4);
  102.     b3.addActionListener( new ActionListener() {
  103.       public void actionPerformed(ActionEvent e) {
  104.           wyswietlHistorieRachunkow("normalne");
  105.       }
  106.     });
  107.     p.add(b4);
  108.     add(p, "North"); 
  109.  
  110.     pack();
  111.     setVisible(true);
  112.   }
  113.  
  114.   private void setColor(final Color color, final Component ... comps) {
  115.  
  116.         for (Component c : comps) c.setBackground(color);
  117.  
  118.       }
  119.  
  120.   private void wyswietlHistorieRachunku(String numer_konta) {
  121.         report.setText(" "); 
  122.         //report.append("Numer konta: " +numer_konta +'\n');
  123.         report.append("Historia Rachunku: " +'\n');
  124.         readFile(numer_konta);
  125.       }
  126.  
  127.   private void wyswietlHistorieRachunkow(String rodzaj_konta) {
  128.         report.setText(" "); 
  129.         //report.append("Numer konta: " +numer_konta +'\n');
  130.  
  131.         String[] pliki=listDir("konta");
  132.         Map<Integer,String> map = new HashMap<Integer,String>();
  133.         map=readFiles(pliki,rodzaj_konta);
  134.         System.out.println(map.size());
  135.         for(int i=0;i<map.size();i++){
  136.             System.out.println(map.get(i));
  137.         wyswietlHistorieRachunku(map.get(i));}
  138.  
  139.  
  140.       }    
  141.  
  142.  
  143.  private String[] listDir(String directory)    {
  144.       File dir = new File(directory);
  145.         String[] files = dir.list();
  146.         if(files == null){
  147.           System.out.println("Specified directory does not exist or is not a directory.");
  148.           System.exit(0);
  149.         }
  150.  
  151.      return files;
  152.  }
  153.  
  154.  private Map<Integer,String> readFiles(String[] file_names,String rodzaj_konta)    {
  155. Map<Integer,String> map = new HashMap<Integer,String>();
  156.  
  157. String wczytane_dane=null;
  158.      for(int i = 0;i<file_names.length;i++){
  159.          System.out.println(i+"file_names[i] :"+file_names[i]);
  160.        boolean jest_rodzaj_konta=false; 
  161.        int j=0;
  162.        try{
  163.         BufferedReader in = new BufferedReader(new FileReader("konta//"+file_names[i]));
  164.  
  165.         String line = in.readLine();
  166.         //System.out.println("line"+line);
  167.  
  168.         while (line !=null) {
  169.             wczytane_dane.concat(line);
  170.             line = in.readLine();
  171.             System.out.println("wczytane_dane"+wczytane_dane);
  172.         }
  173.  
  174.        // System.out.println("rodzaj_konta"+rodzaj_konta);
  175.         if (wczytane_dane.indexOf(rodzaj_konta) > 0) {jest_rodzaj_konta =true;}
  176.         //System.out.println("jest_rodzaj_konta"+jest_rodzaj_konta);
  177.         if(jest_rodzaj_konta){
  178.         String numer_konta=null;
  179.         String[] dane=wczytane_dane.split(":");
  180.         numer_konta=dane[1];
  181.         System.out.println("numer_konta(w ifie)"+numer_konta);
  182.         //report.append(numer_konta +'\n');
  183.         map.put(j,numer_konta);
  184.         j++;
  185.         }
  186.         in.close();
  187.        }catch (FileNotFoundException e) {
  188.              System.err.println("File not found!)");
  189.        }catch (IOException e) {
  190.                System.err.println(e);
  191.        }catch (Exception ex)  {
  192.        ex.printStackTrace();  }    
  193.       wczytane_dane=null;
  194.      }
  195.  
  196.  
  197.  
  198.  
  199.      return map;
  200. }
  201.  
  202.  
  203.  
  204.  
  205. private void readFile(String numer_konta){
  206.  
  207.     try{
  208.  
  209.  
  210.         BufferedReader br = new BufferedReader(new FileReader("konta//"+numer_konta+".txt"));
  211.  
  212.  
  213.  
  214.  
  215.         String[] historia = new String[6];;
  216.         String strLine = br.readLine();
  217.  
  218.       int n=0;
  219.       while((strLine = br.readLine()) != null)   {   
  220.       historia[n]=strLine; 
  221.        n++;
  222.       }
  223.  
  224.  
  225.       for (String h : historia)
  226.           report.append(h +'\n');
  227.  
  228.  
  229.  
  230.         br.close();
  231.       }catch (FileNotFoundException e) {
  232.             System.err.println("Nie ma takiego konta(File not found!)");
  233.       }catch (IOException e) {
  234.               System.err.println(e);
  235.       }catch (Exception ex)  {
  236.       ex.printStackTrace();  }    
  237.  
  238.  
  239.  
  240.  
  241. }      
  242. }
  243.  
Mar 12 '07 #15
r035198x
13,262 8TB
Change
Expand|Select|Wrap|Line Numbers
  1. String wczytane_dane=null;
  2.  
to
Expand|Select|Wrap|Line Numbers
  1. String wczytane_dane="";
Mar 12 '07 #16
oll3i
679 512MB
i did System.out.println(file_names[i] )to see if the file names are okey and they are
i also did System.out.println(wczytane_dane) to see if it concatenates the lines to wczytane_dane it doesnt am i using the right function

as u can see below it does display the lines from file (line imie:Jan)
and i still get NullPointerException

thank u for being there


0file_names[i] :3810204455668897.txt
java.lang.NullPointerException
at konta_bankowe.Interfejs.readFiles(Interfejs.java)
at konta_bankowe.Interfejs.wyswietlHistorieRachunkow( Interfejs.java)
at konta_bankowe.Interfejs.access$1(Interfejs.java)
at konta_bankowe.Interfejs$5.actionPerformed(Interfej s.java)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)wczytane_dane
line imie:Jan
wczytane_dane
line nazwisko:Kowalski
wczytane_dane
line firma:XYZ
wczytane_dane
line rodzaj konta:brazowe
wczytane_dane
line balance:0.0
wczytane_dane
line null
1file_names[i] :3810205643457689.txt

at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
2file_names[i] :3810206655442213.txt
java.lang.NullPointerException
at konta_bankowe.Interfejs.readFiles(Interfejs.java)
at konta_bankowe.Interfejs.wyswietlHistorieRachunkow( Interfejs.java)
at konta_bankowe.Interfejs.access$1(Interfejs.java)
at konta_bankowe.Interfejs$5.actionPerformed(Interfej s.java)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
3file_names[i] :3810207981239012.txt
java.lang.NullPointerException
at konta_bankowe.Interfejs.readFiles(Interfejs.java)
at konta_bankowe.Interfejs.wyswietlHistorieRachunkow( Interfejs.java)
at konta_bankowe.Interfejs.access$1(Interfejs.java)
at konta_bankowe.Interfejs$5.actionPerformed(Interfej s.java)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
0
Mar 12 '07 #17
oll3i
679 512MB
i solved it thank u ;] will see when i go further
thank u for ur answers and assistance
Mar 12 '07 #18

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...
13
oll3i
by: oll3i | last post by:
private List<Klient> klienci; m_add_client.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.