NullPointerException

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

    #1

    NullPointerException

    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.NullP ointerException
    at konta_bankowe.I nterfejs.readFi le(Interfejs.ja va)
    at konta_bankowe.I nterfejs.wyswie tlHistorieRachu nku(Interfejs.j ava)
    at konta_bankowe.I nterfejs.access $0(Interfejs.ja va)
    at konta_bankowe.I nterfejs$1.acti onPerformed(Int erfejs.java)
    at javax.swing.Abs tractButton.fir eActionPerforme d(Unknown Source)
    at javax.swing.Abs tractButton$Han dler.actionPerf ormed(Unknown Source)
    at javax.swing.Def aultButtonModel .fireActionPerf ormed(Unknown Source)
    at javax.swing.Def aultButtonModel .setPressed(Unk nown Source)
    at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Unk nown Source)
    at java.awt.Compon ent.processMous eEvent(Unknown Source)
    at javax.swing.JCo mponent.process MouseEvent(Unkn own Source)
    at java.awt.Compon ent.processEven t(Unknown Source)
    at java.awt.Contai ner.processEven t(Unknown Source)
    at java.awt.Compon ent.dispatchEve ntImpl(Unknown Source)
    at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
    at java.awt.Compon ent.dispatchEve nt(Unknown Source)
    at java.awt.Lightw eightDispatcher .retargetMouseE vent(Unknown Source)
    at java.awt.Lightw eightDispatcher .processMouseEv ent(Unknown Source)
    at java.awt.Lightw eightDispatcher .dispatchEvent( Unknown Source)
    at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
    at java.awt.Window .dispatchEventI mpl(Unknown Source)
    at java.awt.Compon ent.dispatchEve nt(Unknown Source)
    at java.awt.EventQ ueue.dispatchEv ent(Unknown Source)
    at java.awt.EventD ispatchThread.p umpOneEventForF ilters(Unknown Source)
    at java.awt.EventD ispatchThread.p umpEventsForFil ter(Unknown Source)
    at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(Unknown Source)
    at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
    at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
    at java.awt.EventD ispatchThread.r un(Unknown Source)




    and the code is
    Code:
    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();
    
    }
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    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).
    Code:
    Windows:
    C:\\Windows\\....
    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.

    Code:
    Linux:
    /home/user/docs/yourfile
    Once again, you might need to backwhack that...

    Try those, see if you are actually pointing to the correct location.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by oll3i
      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.NullP ointerException
      at konta_bankowe.I nterfejs.readFi le(Interfejs.ja va)
      at konta_bankowe.I nterfejs.wyswie tlHistorieRachu nku(Interfejs.j ava)
      at konta_bankowe.I nterfejs.access $0(Interfejs.ja va)
      at konta_bankowe.I nterfejs$1.acti onPerformed(Int erfejs.java)
      at javax.swing.Abs tractButton.fir eActionPerforme d(Unknown Source)
      at javax.swing.Abs tractButton$Han dler.actionPerf ormed(Unknown Source)
      at javax.swing.Def aultButtonModel .fireActionPerf ormed(Unknown Source)
      at javax.swing.Def aultButtonModel .setPressed(Unk nown Source)
      at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Unk nown Source)
      at java.awt.Compon ent.processMous eEvent(Unknown Source)
      at javax.swing.JCo mponent.process MouseEvent(Unkn own Source)
      at java.awt.Compon ent.processEven t(Unknown Source)
      at java.awt.Contai ner.processEven t(Unknown Source)
      at java.awt.Compon ent.dispatchEve ntImpl(Unknown Source)
      at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
      at java.awt.Compon ent.dispatchEve nt(Unknown Source)
      at java.awt.Lightw eightDispatcher .retargetMouseE vent(Unknown Source)
      at java.awt.Lightw eightDispatcher .processMouseEv ent(Unknown Source)
      at java.awt.Lightw eightDispatcher .dispatchEvent( Unknown Source)
      at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
      at java.awt.Window .dispatchEventI mpl(Unknown Source)
      at java.awt.Compon ent.dispatchEve nt(Unknown Source)
      at java.awt.EventQ ueue.dispatchEv ent(Unknown Source)
      at java.awt.EventD ispatchThread.p umpOneEventForF ilters(Unknown Source)
      at java.awt.EventD ispatchThread.p umpEventsForFil ter(Unknown Source)
      at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(Unknown Source)
      at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
      at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
      at java.awt.EventD ispatchThread.r un(Unknown Source)




      and the code is

      package konta_bankowe;



      import java.awt.*;
      import java.awt.event. *;
      import java.beans.*;
      import java.io.Buffere dReader;
      import java.io.DataInp utStream;
      import java.io.FileInp utStream;
      import java.io.FileNot FoundException;
      import java.io.IOExcep tion;
      import java.io.InputSt reamReader;
      import java.text.Numbe rFormat;

      import javax.swing.*;

      import static java.awt.Color. *;
      import static java.lang.Threa d.sleep;
      import static java.lang.Syste m.out;

      public class Interfejs extends JFrame {

      JTextArea report = new JTextArea(25, 40);

      public Interfejs() {
      setDefaultClose Operation(JFram e.DISPOSE_ON_CL OSE);

      setTitle("Progr am kont bankowych(Katar zyna Olbromska).");
      JPanel panel = new JPanel();
      JLabel label = new JLabel("Podaj Number konta :");
      final JTextField numer_konta_fie ld = new JTextField();

      numer_konta_fie ld.setColumns(1 0);
      panel.add(label );
      panel.add(numer _konta_field);
      JButton button = new JButton();
      button.setLabel ("Szukaj");
      button.addActio nListener( new ActionListener( ) {
      public void actionPerformed (ActionEvent e) {

      wyswietlHistori eRachunku(numer _konta_field.ge tText());


      }
      });
      panel.add(butto n);
      getContentPane( ).add(panel, BorderLayout.SO UTH);
      pack();




      report.setBorde r(BorderFactory .createTitledBo rder("Raport z kont"));
      add(new JScrollPane(rep ort));

      JPanel p = new JPanel();

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

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

      }
      });
      p.add(b1);

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

      }
      });
      p.add(b2);

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

      }
      });
      p.add(b3);

      final JButton b4 = new JButton("Normal ne");
      setColor(WHITE, b4);
      b3.addActionLis tener( 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 wyswietlHistori eRachunku(Strin g 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 InputStreamRead er(in));
      String strLine;
      String[] historia = null;


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


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



      in.close();
      }catch (FileNotFoundEx ception e) {
      System.err.prin tln("Nie ma takiego konta(File not found!)");
      }catch (IOException e) {
      System.err.prin tln(e);
      }catch (Exception ex) {
      ex.printStackTr ace(); }




      }
      }



      package konta_bankowe;


      import java.io.*;
      import java.util.HashM ap;
      import java.util.Map;
      import java.applet.App let;
      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("fil eName.txt");
      BufferedReader br = new BufferedReader( fr);

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

      Code:
      String[] historia = null;
      You initialized this array to null.

      Code:
      int n=0;
      while((strLine = br.readLine()) != null) {
      There is no problem here ....

      ...but
      Code:
      historia[n]=strLine;
      You try to assign something into an array which you have initialized to null. This will give the NullPointerExce ption.
      Either initialize the array first
      Code:
      String[] historia = new String[someInteger];
      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.

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #4
        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.I nterfejs.readFi le(Interfejs.ja va)
        at konta_bankowe.I nterfejs.wyswie tlHistorieRachu nku(Interfejs.j ava)
        at konta_bankowe.I nterfejs.access $0(Interfejs.ja va)
        at konta_bankowe.I nterfejs$1.acti onPerformed(Int erfejs.java)
        at javax.swing.Abs tractButton.fir eActionPerforme d(Unknown Source)
        at javax.swing.Abs tractButton$Han dler.actionPerf ormed(Unknown Source)
        at javax.swing.Def aultButtonModel .fireActionPerf ormed(Unknown Source)
        at javax.swing.Def aultButtonModel .setPressed(Unk nown Source)
        at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Unk nown Source)
        at java.awt.Compon ent.processMous eEvent(Unknown Source)
        at javax.swing.JCo mponent.process MouseEvent(Unkn own Source)
        at java.awt.Compon ent.processEven t(Unknown Source)
        at java.awt.Contai ner.processEven t(Unknown Source)
        at java.awt.Compon ent.dispatchEve ntImpl(Unknown Source)
        at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
        at java.awt.Compon ent.dispatchEve nt(Unknown Source)
        at java.awt.Lightw eightDispatcher .retargetMouseE vent(Unknown Source)
        at java.awt.Lightw eightDispatcher .processMouseEv ent(Unknown Source)
        at java.awt.Lightw eightDispatcher .dispatchEvent( Unknown Source)
        at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
        at java.awt.Window .dispatchEventI mpl(Unknown Source)
        at java.awt.Compon ent.dispatchEve nt(Unknown Source)
        at java.awt.EventQ ueue.dispatchEv ent(Unknown Source)
        at java.awt.EventD ispatchThread.p umpOneEventForF ilters(Unknown Source)
        at java.awt.EventD ispatchThread.p umpEventsForFil ter(Unknown Source)
        at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(Unknown Source)
        at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
        at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
        at java.awt.EventD ispatchThread.r un(Unknown Source)

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by oll3i
          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.I nterfejs.readFi le(Interfejs.ja va)
          at konta_bankowe.I nterfejs.wyswie tlHistorieRachu nku(Interfejs.j ava)
          at konta_bankowe.I nterfejs.access $0(Interfejs.ja va)
          at konta_bankowe.I nterfejs$1.acti onPerformed(Int erfejs.java)
          at javax.swing.Abs tractButton.fir eActionPerforme d(Unknown Source)
          at javax.swing.Abs tractButton$Han dler.actionPerf ormed(Unknown Source)
          at javax.swing.Def aultButtonModel .fireActionPerf ormed(Unknown Source)
          at javax.swing.Def aultButtonModel .setPressed(Unk nown Source)
          at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Unk nown Source)
          at java.awt.Compon ent.processMous eEvent(Unknown Source)
          at javax.swing.JCo mponent.process MouseEvent(Unkn own Source)
          at java.awt.Compon ent.processEven t(Unknown Source)
          at java.awt.Contai ner.processEven t(Unknown Source)
          at java.awt.Compon ent.dispatchEve ntImpl(Unknown Source)
          at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
          at java.awt.Compon ent.dispatchEve nt(Unknown Source)
          at java.awt.Lightw eightDispatcher .retargetMouseE vent(Unknown Source)
          at java.awt.Lightw eightDispatcher .processMouseEv ent(Unknown Source)
          at java.awt.Lightw eightDispatcher .dispatchEvent( Unknown Source)
          at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
          at java.awt.Window .dispatchEventI mpl(Unknown Source)
          at java.awt.Compon ent.dispatchEve nt(Unknown Source)
          at java.awt.EventQ ueue.dispatchEv ent(Unknown Source)
          at java.awt.EventD ispatchThread.p umpOneEventForF ilters(Unknown Source)
          at java.awt.EventD ispatchThread.p umpEventsForFil ter(Unknown Source)
          at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(Unknown Source)
          at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
          at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
          at java.awt.EventD ispatchThread.r un(Unknown Source)
          Post the code that you have now using code tags.

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #6
            i fixed it thank u

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by oll3i
              i fixed it thank u
              Good for you.

              Did you use the FileReader for it then?

              Comment

              • oll3i
                Contributor
                • Mar 2007
                • 679

                #8
                but i get another NullPointerExce ption
                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,Str ing> readFiles(Strin g[] file_names,Stri ng rodzaj_konta) {
                Map<Integer,Str ing> map = new HashMap<Integer ,String>();

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

                boolean jest_rodzaj_kon ta=false;
                int j=0;
                try{
                BufferedReader in = new BufferedReader( new FileReader("kon ta//"+file_name s[i]));

                String line = in.readLine();


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

                }


                if (wczytane_dane. indexOf(rodzaj_ konta) > 0) {jest_rodzaj_ko nta =true;}

                if(jest_rodzaj_ konta){
                String numer_konta=nul l;
                String[] dane=wczytane_d ane.split(":");
                numer_konta=dan e[1];


                map.put(j,numer _konta);
                j++;
                }
                in.close();
                }catch (FileNotFoundEx ception e) {
                System.err.prin tln("File not found!)");
                }catch (IOException e) {
                System.err.prin tln(e);
                }catch (Exception ex) {
                ex.printStackTr ace(); }
                wczytane_dane=n ull;
                }




                return map;
                }

                Comment

                • oll3i
                  Contributor
                  • Mar 2007
                  • 679

                  #9
                  it goes through all the files in the directory 2 times

                  yes i used BufferedReader in = new BufferedReader( new FileReader("kon ta//"+file_name s[i]));

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by oll3i
                    but i get another NullPointerExce ption
                    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,Str ing> readFiles(Strin g[] file_names,Stri ng rodzaj_konta) {
                    Map<Integer,Str ing> map = new HashMap<Integer ,String>();

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

                    boolean jest_rodzaj_kon ta=false;
                    int j=0;
                    try{
                    BufferedReader in = new BufferedReader( new FileReader("kon ta//"+file_name s[i]));

                    String line = in.readLine();


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

                    }


                    if (wczytane_dane. indexOf(rodzaj_ konta) > 0) {jest_rodzaj_ko nta =true;}

                    if(jest_rodzaj_ konta){
                    String numer_konta=nul l;
                    String[] dane=wczytane_d ane.split(":");
                    numer_konta=dan e[1];


                    map.put(j,numer _konta);
                    j++;
                    }
                    in.close();
                    }catch (FileNotFoundEx ception e) {
                    System.err.prin tln("File not found!)");
                    }catch (IOException e) {
                    System.err.prin tln(e);
                    }catch (Exception ex) {
                    ex.printStackTr ace(); }
                    wczytane_dane=n ull;
                    }




                    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.c oncat(line); will give a null pointer.

                    the second source is not definite and may be the arguments you are supplying.
                    Change the reading to
                    Code:
                     String line = in.readLine(); 
                    	  while (line != null) {
                    		   wczytane_dane.concat(line);
                    			line = in.readLine();
                    	  }

                    Comment

                    • oll3i
                      Contributor
                      • Mar 2007
                      • 679

                      #11
                      i moved the line wczytane_dane.c oncat(line);
                      before
                      line = in.readLine();

                      but i still get NullPointerExce ption

                      Comment

                      • oll3i
                        Contributor
                        • Mar 2007
                        • 679

                        #12
                        it doesnt even seem to read in the line from file cos i did system.out.prin tln to test it and it displays nothing

                        here in this while loop

                        while (line != null) {
                        wczytane_dane.c oncat(line);
                        line = in.readLine();
                        System.out.prin tln("wczytane_d ane"+wczytane_d ane);
                        }

                        Comment

                        • oll3i
                          Contributor
                          • Mar 2007
                          • 679

                          #13
                          i forgot the code tags sorry

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by oll3i
                            it doesnt even seem to read in the line from file cos i did system.out.prin tln to test it and it displays nothing

                            here in this while loop

                            while (line != null) {
                            wczytane_dane.c oncat(line);
                            line = in.readLine();
                            System.out.prin tln("wczytane_d ane"+wczytane_d ane);
                            }
                            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.

                            Comment

                            • oll3i
                              Contributor
                              • Mar 2007
                              • 679

                              #15
                              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(i nto 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_kon ta 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 ;]



                              Code:
                              package konta_bankowe;
                              
                              
                              
                              import java.awt.*;
                              import java.awt.event.*;
                              import java.beans.*;
                              import java.io.BufferedReader;
                              import java.io.DataInputStream;
                              import java.io.File;
                              import java.io.FileInputStream;
                              import java.io.FileNotFoundException;
                              import java.io.FileReader;
                              import java.io.IOException;
                              import java.io.InputStreamReader;
                              import java.text.NumberFormat;
                              import java.util.HashMap;
                              import java.util.HashSet;
                              import java.util.Map;
                              import java.util.Set;
                              
                              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) {
                                  	  wyswietlHistorieRachunkow("zlote");
                                    }
                                  });
                                  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) {
                                  	  wyswietlHistorieRachunkow("srebrne");
                                    }
                                  });
                                  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) {
                                  	  wyswietlHistorieRachunkow("brazowe");
                                    }
                                  });
                                  p.add(b3);
                                  
                                  final JButton b4 = new JButton("Normalne");
                                  setColor(WHITE, b4);
                                  b3.addActionListener( new ActionListener() {
                                    public void actionPerformed(ActionEvent e) {
                                  	  wyswietlHistorieRachunkow("normalne");
                                    }
                                  });
                                  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 wyswietlHistorieRachunkow(String rodzaj_konta) {
                              	    report.setText(" "); 
                              	    //report.append("Numer konta: " +numer_konta +'\n');
                              	    
                              	    String[] pliki=listDir("konta");
                              	    Map<Integer,String> map = new HashMap<Integer,String>();
                              	    map=readFiles(pliki,rodzaj_konta);
                              	    System.out.println(map.size());
                              	    for(int i=0;i<map.size();i++){
                              	    	System.out.println(map.get(i));
                              	    wyswietlHistorieRachunku(map.get(i));}
                              	    
                              	    
                              	  }	
                                
                                
                               private String[] listDir(String directory)	{
                              	  File dir = new File(directory);
                              	    String[] files = dir.list();
                              	    if(files == null){
                              	      System.out.println("Specified directory does not exist or is not a directory.");
                              	      System.exit(0);
                              	    }
                              
                              	 return files;
                               }
                               
                               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++){
                                  	 System.out.println(i+"file_names[i] :"+file_names[i]);
                                     boolean jest_rodzaj_konta=false; 
                                     int j=0;
                                     try{
                              	    BufferedReader in = new BufferedReader(new FileReader("konta//"+file_names[i]));
                              	   
                              	    String line = in.readLine();
                              	    //System.out.println("line"+line);
                              	    
                              	    while (line !=null) {
                              	    	wczytane_dane.concat(line);
                              	    	line = in.readLine();
                              	        System.out.println("wczytane_dane"+wczytane_dane);
                              	    }
                              	  
                              	   // System.out.println("rodzaj_konta"+rodzaj_konta);
                              	    if (wczytane_dane.indexOf(rodzaj_konta) > 0) {jest_rodzaj_konta =true;}
                              	    //System.out.println("jest_rodzaj_konta"+jest_rodzaj_konta);
                              	    if(jest_rodzaj_konta){
                              	    String numer_konta=null;
                              	    String[] dane=wczytane_dane.split(":");
                              	    numer_konta=dane[1];
                              	    System.out.println("numer_konta(w ifie)"+numer_konta);
                              	    //report.append(numer_konta +'\n');
                              	    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;
                              }
                               
                               
                               
                               
                              private void readFile(String numer_konta){
                              	
                              	try{
                              	    
                              	    
                              	    BufferedReader br = new BufferedReader(new FileReader("konta//"+numer_konta+".txt"));
                              		       
                              	    
                              	    
                              	    
                              	    String[] historia = new String[6];;
                              	    String strLine = br.readLine();
                              	
                              	  int n=0;
                              	  while((strLine = br.readLine()) != null)   {   
                              	  historia[n]=strLine; 
                              	   n++;
                              	  }
                              	  
                              	  
                              	  for (String h : historia)
                              		  report.append(h +'\n');
                              
                              	
                              	  
                              	    br.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();  }	
                              	
                              	
                              	
                              	
                              }	  
                              }

                              Comment

                              Working...