Results 1 to 6 of 6
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default a strange null pointer problem

    Two time I ask for staticinfo.length, the first time it iis ok
    and the second time it turns into a null pointer why?
    Please any help (sorry for the previous confusion)
    See marks in the code --->


    Java Code:
    /*
     *
     * The program begins searching the file "RoomDataFile.txt"
     * When found the static room dat is set (index the roomnumber)
     *
     *
     *
     *
     *
     */
    
    package roomassigment;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.StringTokenizer;
    import javax.swing.*;
    import javax.swing.border.Border;
    
    /**
     *
     * 
     */
    class DeskMonitor extends JFrame  {
        private JPanel mainPanel, infopanel;
        private JLabel label, infolabel;
        private int index, xgridPanel, ygridPanel, xwindowsize, ywindowsize;
        private DynamicRoomInfo[] dynamicinfo;
        private StaticRoomInfo[] staticinfo;
        private Boolean[] roomoccupied;
        private SingleRoomPanel singleroompanel;
        private ImageIcon icon1, icon2, icon3;
        private MouseHandler Mhandler;
                StudentMonitor smonitor;
    
        public DeskMonitor() throws FileNotFoundException  {
    
          readDataFile(); // important dat file from DeskPanelSetup
    
          // the class arrays that contain all info
          dynamicinfo = new DynamicRoomInfo[index]; // initialize the dynamic
          for (int i = 0; i < dynamicinfo.length; i++) { // roominfopanel array
              dynamicinfo[i] = new DynamicRoomInfo();
          }
    
          staticinfo = new StaticRoomInfo[index]; // initialize the static
          for (int i = 0; i < staticinfo.length; i++) { // roominfopanel array         <---- here it is ok
              staticinfo[i] = new StaticRoomInfo();
          }
          System.out.println(staticinfo.length);
          roomoccupied = new Boolean[index];
    
          icon1 = createImageIcon("http://www.java-forums.org/images/keyboard.gif");
          icon2 = createImageIcon("http://www.java-forums.org/images/lic2.gif");
          icon3 = createImageIcon("http://www.java-forums.org/images/musicstand.gif");
    
          setTitle("Principle Desk Monitor");
          setBounds(100, 100, xwindowsize, ywindowsize); // window size set by file
          mainPanel = new JPanel(new BorderLayout());
          Mhandler = new MouseHandler();
          // object which constructs ind room panels
          singleroompanel = new SingleRoomPanel();
    
          mainPanel.add(CreatePanels(), BorderLayout.CENTER);
          mainPanel.add(createTopInfoPanel(), BorderLayout.NORTH);
    
          // set a listener for some keys
          mainPanel.setFocusable(true);
          //mainPanel.requestFocusInWindow();
          Container contentPane = getContentPane();
          contentPane.add(mainPanel);
          mainPanel.addKeyListener (new KeyAdapter() { // read some key events
                public void keyPressed(KeyEvent e) {
                    int keyCode = e.getKeyCode();
                  //  System.out.println("You Pressed " + keyCode);
                    if (keyCode == KeyEvent.VK_SPACE)  {
                       System.out.println(" spacekey");
                       // activate studentmonitor
                       smonitor = new StudentMonitor();
                    }
                  // this simulates the studentroom card
                    if (keyCode == 84)  {
                       System.out.println(" T key pressed");
                       System.out.println(smonitor.rdialog.getFirstName());
                       System.out.println(smonitor.rdialog.getLastName());
                    }
                }
          });
          
          
        }
    
    
        public JPanel CreatePanels() {
    
            JPanel roomPanel = new JPanel();
            roomPanel.setLayout(new GridLayout(xgridPanel,ygridPanel));
            
            for (int i = 0; i <index; i++) {
                    String room = staticinfo[i].getRoomNumber();
                    JPanel rp = singleroompanel.CreateSingleRoomPanel(new
                                                  JPanel(), room);
                    staticinfo[i].setRoomNumber(room);
                    staticinfo[i].setPanelRef(rp);
    
                    if (staticinfo[index].getPianoRoom());
                    
                    rp.addMouseListener(Mhandler);
                    roomPanel.add(rp);
                
            }
            
            return roomPanel;
         }
    
    
         public JPanel createTopInfoPanel()  {
             infopanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
             infolabel = new JLabel(icon2);
             infopanel.add(infolabel);
             return infopanel;
         }
    
         private void readDataFile() throws FileNotFoundException  {
               try {
                   BufferedReader in = new BufferedReader(new
                   FileReader("RoomDataFile.txt"));
                   readData(in); 
                   in.close();
                   System.out.println(" file loaded! ");
               }
    
               catch(IOException exception)  {
                   exception.printStackTrace();
                   System.out.println(" file NOT loaded! ");
               }
        }
    
         public void readData(BufferedReader in)
                                        throws IOException  {
                String s = in.readLine();
                StringTokenizer t = new StringTokenizer(s, ";.");
    
                t.nextToken(); // the indentifeir still to set
                index = Integer.parseInt(t.nextToken());
                xgridPanel = Integer.parseInt(t.nextToken());
                ygridPanel = Integer.parseInt(t.nextToken());
                xwindowsize = Integer.parseInt(t.nextToken());
                ywindowsize = Integer.parseInt(t.nextToken());
                System.out.println(staticinfo.length);                  <----- here it is a null pointer
                   for (int i = 0; i < staticinfo.length; i++) {
                      s = in.readLine();
                      t.nextToken();
    
                    staticinfo[i].setRoomNumber(t.nextToken());
                    staticinfo[i].setPianoRoom(Boolean.parseBoolean(t.nextToken()));
                    staticinfo[i].setChamberMusic(Boolean.parseBoolean(t.nextToken()));
                    staticinfo[i].setDisable(Boolean.parseBoolean(t.nextToken()));
                }
         }
    
         public class DynamicRoomInfo  { // principle class holds all room info
           
           private String studentfirstname, studentlastname;
           private boolean piano, roomfree, reserved;
           private long endtime;
    
           public void setStudentFirstName(String sfn)  {
               studentfirstname = sfn;
           }
           public String getStudentFirstName()  {
               return studentfirstname;
           }
           public void setStudentLastName(String sn)  {
               studentlastname = sn;
           }
    
           public String getStudentLastName()  {
               return studentlastname;
           }
    
            public void setPianoRoom(Boolean pf)  {
               piano = pf;
           }
           public Boolean getPianoRoom()  {
               return piano;
           }
    
            public void setFreeRoom(Boolean rf)  {
               roomfree = rf;
           }
           public Boolean getFreeRoom()  {
               return roomfree;
           }
    
           public void setEndTime(long et)  {
               endtime = et;
           }
           public long getEndTime()  {
               return endtime;
           }
           public void setReserved(Boolean rs)  {
               reserved = rs;
           }
           public Boolean getReserved()  {
               return reserved;
           }
    
         }
    
         public class StaticRoomInfo    {
              private JPanel panelref;
              private String roomnumber;
              private boolean chamberm;
              private boolean piano;
              private boolean disable;
    
              public void setPanelRef(JPanel jp)  {
                   panelref = jp;
              }
              public JPanel getPanelRef()  {
                  return panelref;
              }
    
              public void setRoomNumber(String rn)  {
                      roomnumber = rn;
              }
              public String getRoomNumber()  {
                      return roomnumber;
              }
    
              public void setPianoRoom(Boolean pf)  {
                    piano = pf;
              }
              public Boolean getPianoRoom()  {
                    return piano;
              }
    
              public void setChamberMusic(Boolean ch)  {
                    chamberm = ch;
              }
              public Boolean getChamberMusic()  {
                    return chamberm;
              }
    
              public void setDisable(Boolean dis)  {
                    disable = dis;
              }
              public Boolean getDisable()  {
                    return disable;
              }
         }
    
           // input roomnumber output index
           public int getRoomNumberIndex(String roomnum)  {
           int index =-1;
           for (int i = 0; i < staticinfo.length; i++) {
               if (staticinfo[i].getRoomNumber() == roomnum)
                   index = i;
           }
               return index;
         }
    
    
        protected static ImageIcon createImageIcon(String path) { // loads the icons
            java.net.URL imgURL = StudentMonitor.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    
        public class MouseHandler implements MouseListener {
            
            public void mousePressed(MouseEvent e) {
                   // System.out.println(" mouse pressed at " + e);
                     //   e.getClickCount()
            }
    
            public void mouseReleased(MouseEvent e) {
    
            }
    
            public void mouseEntered(MouseEvent e) {
               
            }
    
            public void mouseExited(MouseEvent e) {
    
            }
    
            public void mouseClicked(MouseEvent e) {
                int ii=0;
                if (e.getClickCount() >= 2) {
                    // roominfopanrl array
                    for (int i = 0; i < staticinfo.length; i++) {
                            if (e.getSource() == staticinfo[i].getPanelRef()) {
                                System.out.println(" bingo " + i);
                                ii = i;
                        }
    
                    }
                    Font myFont = new Font("SansSerif", Font.PLAIN, 14);
                        Color myColor = Color.RED;
    
                        Border etched = BorderFactory.createEtchedBorder(myColor,myColor);
                        Border titled = BorderFactory.createTitledBorder(etched,
                          "ROOM " + staticinfo[ii].getRoomNumber(),
                                0, 0, myFont, myColor);
                        staticinfo[ii].getPanelRef().setBorder(titled);
                }
            }
       }
    }
    Last edited by willemjav; 03-29-2013 at 11:10 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: a strange null pointer problem

    It is because you call readDataFile before you allocate storage to staticinfo. So staticinfo is null. Move your initial assignment to staticinfo before calling readDataFile.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a strange null pointer problem

    Jim didn't I do just that from line 58?

    Java Code:
     staticinfo = new StaticRoomInfo[index]; // initialize the static
          for (int i = 0; i < staticinfo.length; i++) { // roominfopanel array         <---- here it is ok
              staticinfo[i] = new StaticRoomInfo();
          }

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a strange null pointer problem

    An other question: how goes a in.readLine(); to a next line?
    Or does each in.readLine(); jumps automatically to the next line?

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: a strange null pointer problem

    Ya got it should get the method call more down....
    thanks jim

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: a strange null pointer problem

    It just reads in all characters up to but not including the line terminator. Then it is positioned for the next line.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 13
    Last Post: 12-08-2011, 02:21 AM
  2. null pointer exception problem
    By dylandrop in forum New To Java
    Replies: 12
    Last Post: 04-02-2011, 07:30 PM
  3. Problem with null pointer exception?
    By dunafrothint in forum AWT / Swing
    Replies: 3
    Last Post: 11-11-2009, 03:34 PM
  4. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 02:10 PM
  5. Integration Problem-Null pointer Exception
    By uhdam in forum AWT / Swing
    Replies: 0
    Last Post: 04-07-2009, 11:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •