Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Null pointer exception

  1. #1
    Junior Member Wrathgarr's Avatar
    Join Date
    Apr 2010
    Location
    Colorado
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Null pointer exception

    Hey folks,
    So I've been working a little program that draws maps. It reads in a CSV file with city names, coordinates, distances, and interconnecting highways. Its supposed to draw red lines (highways) between black dots (cities). However, I keep getting a null pointer exception, and I can't figure it out! here's my code so far, and I'll attach the CSV.

    here's the main program:

    package Maps2;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.io.File;
    import javax.swing.JFileChooser;
    import javax.swing.JPanel;
     
    /**
     *
     * @author stu696084
     */
    public class Map extends javax.swing.JFrame {
     
      JFileChooser fileChooser = new JFileChooser();
      public City start;
      public City end;
        /** Creates new form Map */
        public Map() {
            initComponents();}
            public class MyPanel extends JPanel {
      @Override
            @SuppressWarnings("static-access")
           public void paint(Graphics g) {
               super.paint(g);
               for(City c: City.cities) {
                     Color z= Color.BLACK;
     
                   if (c == start){
                       z = Color.RED;
     
     
                   }
                   else if (c == end) {
                         z = Color.BLUE;
                   }
                     g.setColor(z);
                     g.fillOval((int)start.p.px, (int)start.p.py, 15, 15);
                     g.setColor(Color.RED);
                     g.drawLine((int)start.p.px, (int)start.p.py, (int)end.p.px, (int)end.p.py);
               }
     
               // Place code to draw the map here
               }
           }
     
     
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            theMap = new MyPanel();
            loadMap = new javax.swing.JButton();
            showMap = new javax.swing.JButton();
            showDirections = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            theMap.setBackground(new java.awt.Color(102, 204, 0));
            theMap.setPreferredSize(new java.awt.Dimension(1000, 650));
            theMap.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    theMapMouseClicked(evt);
                }
            });
     
            org.jdesktop.layout.GroupLayout theMapLayout = new org.jdesktop.layout.GroupLayout(theMap);
            theMap.setLayout(theMapLayout);
            theMapLayout.setHorizontalGroup(
                theMapLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 1000, Short.MAX_VALUE)
            );
            theMapLayout.setVerticalGroup(
                theMapLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 650, Short.MAX_VALUE)
            );
     
            loadMap.setText("Load Map");
            loadMap.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    loadMapActionPerformed(evt);
                }
            });
     
            showMap.setText("Show Map");
            showMap.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    showMapActionPerformed(evt);
                }
            });
     
            showDirections.setText("Show Directions");
            showDirections.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    showDirectionsActionPerformed(evt);
                }
            });
     
            jLabel1.setText("jLabel1");
     
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(50, 50, 50)
                            .add(theMap, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(52, 52, 52)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(showMap)
                                .add(loadMap)
                                .add(showDirections)))
                        .add(layout.createSequentialGroup()
                            .add(464, 464, 464)
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 310, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(80, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(6, 6, 6)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(loadMap)
                            .add(34, 34, 34)
                            .add(showMap)
                            .add(29, 29, 29)
                            .add(showDirections))
                        .add(theMap, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(41, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void loadMapActionPerformed(java.awt.event.ActionEvent evt) {                                        
          int retval = fileChooser.showOpenDialog(this);
          if (retval == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
            Input.readFile(file);
          }
            // TODO add your handling code here:
        }                                       
     
        private void showMapActionPerformed(java.awt.event.ActionEvent evt) {                                        
     
     
                for (City c : City.cities) {
                    System.out.println(c.name + " Is at " + c.p.px + ", " + c.p.py);
     
     
                    for (Road r : c.roads) {
                        System.out.println("  Road " + r.name + " Goes to " + r.endCity.name + " (" + r.length + ", " + r.time + ")");
     
                    }
     
     
            }  // TODO add your handling code here:
            // TODO add your handling code here:
        }                                       
     
        private void showDirectionsActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
     
        private void theMapMouseClicked(java.awt.event.MouseEvent evt) {                                    
           for(City c : City.cities) {
     
               if(Point.close(evt.getX(), evt.getY())==true) {
                   c = start;
               }
               else if (c==start) {
                   c = end;
               }
           }
            // TODO add your handling code here:
        }                                   
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Map().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JLabel jLabel1;
        private javax.swing.JButton loadMap;
        private javax.swing.JButton showDirections;
        private javax.swing.JButton showMap;
        private javax.swing.JPanel theMap;
        // End of variables declaration                   
     
    }

    Here's the City and Road classes
    package Maps2;
     
    import java.util.ArrayList;
     
    /**
     *
     * @author stu696084
     */
    public class City {
     
        public Point p;
        public String name;
        public static ArrayList<City> cities = new ArrayList<City>();
        public ArrayList<Road> roads = new ArrayList<Road>();
     
        public City(String name, int x, int y) {
            this.name = name;
            this.p = new Point(x, y);
     
            cities.add(this);
     
        }
     
        public boolean isCity(String s) {
            return s.equals(name);
        }
     
        public static City findCity(String s) {
            for (City c : cities) {
                if (c.isCity(s)) {
                    return c;
                }
            }
            return null;
        }
     
        public static void Clear() {
            City.cities.clear();
        }
    }

    package Maps2;
     
    /**
     *
     * @author stu696084
     */
    public class Road {
     
        public City startCity, endCity;
        public int length, time;
        public String name;
     
        public Road(String name, City startCity, City endCity, int length, int time) {
            this.startCity = startCity;
            this.endCity = endCity;
            this.length = length;
            this.time = time;
            this.name = name;
        }
     
        @Override
        public String toString() {
            return (startCity + "and" + endCity);
        }
    }

    and finally the Input and Point classes:

    package Maps2;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
     
    /**
     *
     * @author stu696084
     */
    public class Input {
     
        public static void readFile(File file) {
            City.Clear();
     
            try {
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String s;
                Boolean road = false;
                while ((s = reader.readLine()) != null) {
                    String[] strings = s.split(",");
                    if (strings.length == 0) {
                        System.out.println("nothin");
                        road = true;
                    } else if (road == false) {
                        City c = new City(strings[0], Integer.parseInt(strings[1]), Integer.parseInt(strings[2]));
     
     
     
                    } else {
                        Road r = new Road(strings[0], City.findCity(strings[1]), City.findCity(strings[2]), Integer.parseInt(strings[3]), Integer.parseInt(strings[4]));
                        Road r2 = new Road(strings[0], City.findCity(strings[2]), City.findCity(strings[1]), Integer.parseInt(strings[3]), Integer.parseInt(strings[4]));
                        r.startCity.roads.add(r);
                        r2.startCity.roads.add(r2);
                    }
     
                }
            } catch (Exception e) {
                System.out.println("File Error");
            }
        }
     
    }

    package Maps2;
     
    /**
     *
     * @author stu696084
     */
    public class Point {
     
        static double px, py;
    double x, y;
        @SuppressWarnings("static-access")
        public Point(double x, double y) {
            this.x = x;
            this.y = y;
            this.px = x * 2;
            this.py = y * 1.3 - 30;
        }
     
        public static boolean close(double mousex, double mousey) {
            return (mousex - px) * mousex - px + (mousey - py) * (mousey - py) < 25;
     
        }
     
        @Override
        public String toString() {
            return "(" + x + "," + y + ")";
        }
    }


    And I think the CSV is attached, but let me know if it doesn't work for you.
    Any help on this would be great guys, thanks a lot!


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Null pointer exception

    Where are you exactly getting the Null pointer exception, it's too difficult to trace the whole program for me as i have not much time. Thanks for if you will be able to tell that in what area, this exception is being occured???

Similar Threads

  1. setText() NULL pointer excception problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2010, 08:32 PM
  2. the infamous null pointer applet problem
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 22nd, 2010, 08:09 PM
  3. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  4. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM