[Java] Another question on constructor

  1. class LargeGrid
  2. {
  3.    public LargeGrid()
  4.    {
  5.       id = 0;
  6.       startx = 0;
  7.       starty = 0;
  8.       w = 0;
  9.       h = 0;
  10.    }

  11.    int id;
  12.    int startx;
  13.    int starty;
  14.    int w;
  15.    int h;
  16. }


  17. class cPathFinder
  18. {
  19.     boolean     aGrid[][];
  20.      PathTileNodeType ptn;
  21.      LargeGrid   alg[];
  22.      int LargeGridCount;
  23.      int iHeight;
  24.      int iWidth;



  25.     public cPathFinder() { }
  26.     public cPathFinder(int width, int height)
  27.     {
  28.       ptn = new PathTileNodeType();
  29.       aGrid = new boolean[width][height];
  30.       iHeight = height;
  31.       iWidth = width;


  32.       alg = new LargeGrid[100];

  33.       String trystr = new String();
  34.       trystr = Integer.toString(alg[0].startx);  <<<< alg seems to be null
  35.       System.out.println(trystr);
複製代碼
//////////////////
D:\PATHFI~1>java PathFinder
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at cPathFinder.<init>(PathFinder.java:533)
        at MainFrame.mousePressed(PathFinder.java:287)
        at java.awt.AWTEventMulticaster.mousePressed(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(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown 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.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)


Anybody knows why?

Thanks

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
atempt to do something with object reference which points to nothing

TOP

原帖由 阿水 於 2009-9-8 21:15 發表
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
atempt to do something with object reference which points to nothing


Thanks.... i just can't find the bug...

TOP

你個PathFinder.java有無其他class?
我只看到cPathFinder,看不到class PathFinder
cPathFinder同PathFinder甚麼關係?

似乎看不到甚麼問題,為甚麼你會覺得是這一段code有問題?

題外話
String trystr = new String();
如沒記錯,String是immutable的,這個new String()是多餘...

[ 本帖最後由 梁炳 於 2009-9-8 21:38 編輯 ]

TOP

  1. alg = new LargeGrid[100];
複製代碼
It only allocates a array to hold 100 LargeGrid object but the individual object is not allocated.

So you need to allocate the 100 objects:
  1. alg = new LargeGrid[100];
  2. for (I=0; i<100; ++i) {
  3.     alg[i] = new LargeGrid();
  4. }
複製代碼

TOP

Thanks everybody... alw is right....
The individual object has to be instantiate before use
Jack

TOP

jack, seems you write C++ before, right?
Me too....it's sometimes hard to change the mindset to JAVA

[ 本帖最後由 梁炳 於 2009-9-9 17:41 編輯 ]

TOP

原帖由 梁炳 於 2009-9-9 17:41 發表
jack, seems you write C++ before, right?
Me too....it's sometimes hard to change the mindset to JAVA



ha ha ha.... you're right.... I am still learning Java
Thanks

TOP