Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

tictactoe help

 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so far i have this,


where are the errors im getting, please help0.0

----jGRASP exec: javac -g tictactoe.java

tictactoe.java:13: <identifier> expected
x = getImage(getCodeBase(),"Black_x.png");
^
tictactoe.java:14: <identifier> expected
o = getImage(getCodeBase(),"o.gif");
^
tictactoe.java:15: <identifier> expected
e = getImage(getCodeBase(),"empty.png");
^
tictactoe.java:16: <identifier> expected
icE=new ImageIcon(e);
^
tictactoe.java:19: <identifier> expected
b1= new JButton(icE);
^
tictactoe.java:20: <identifier> expected
b2= new JButton(icE);
^
tictactoe.java:21: <identifier> expected
b3= new JButton(icE);
^
tictactoe.java:22: <identifier> expected
b4= new JButton(icE);
^
tictactoe.java:23: <identifier> expected
b5= new JButton(icE);
^
tictactoe.java:24: <identifier> expected
b6= new JButton(icE);
^
tictactoe.java:25: <identifier> expected
b7= new JButton(icE);
^
tictactoe.java:26: <identifier> expected
b8= new JButton(icE);
^
tictactoe.java:27: <identifier> expected
b9= new JButton(icE);
^
tictactoe.java:30: <identifier> expected
jlWin= new JLabel(sLetter + " Wins!");
^
tictactoe.java:31: <identifier> expected
jlTie= new JLabel("NO ONE WINS!");
^

please help 0.0 im so confused :-\
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking a quick look at this: line 11 is wrong, it should be String instead of string. Remember that Java is case-sensitive!

You cannot put arbitrary statements, which you have in lines 13 - 31, at class level. You can only add variable, constructor and method declarations (optionally with an initialization value for the variable) at class level. Put the statements from lines 13 - 31 in a constructor or method, or combine them with the declaration of the variables just above.
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you!
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you mean combine them?
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


got rid of all my errors, but now its not running ? it says it didn initialize
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the Java Console in your system tray, there's probably an exception. Post the full stack trace here.
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Check the Java Console in your system tray, there's probably an exception. Post the full stack trace here.

java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Applet.java:152)
at tictactoe.<init>(tictactoe.java:12)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:662)
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks as if the problem is on line 12.
You have made things difficult for yourself by trying to write the whole thing in one go. You ought to do it bit by bit.

Try printing the code base URL and the toString method of the Image to screen; then you can narrow down where the error occurs: is it code base or is it the image?

And the length of your actionPerformed method is a good example of why addActionListener(this) is poor, non-object-oriented programming. Even though there are lots of books which show such code.
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It looks as if the problem is on line 12.
You have made things difficult for yourself by trying to write the whole thing in one go. You ought to do it bit by bit.

Try printing the code base URL and the toString method of the Image to screen; then you can narrow down where the error occurs: is it code base or is it the image?

And the length of your actionPerformed method is a good example of why addActionListener(this) is poor, non-object-oriented programming. Even though there are lots of books which show such code.


java.lang.NullPointerException

at java.applet.Applet.getCodeBase(Applet.java:152)
at tictactoe.<init>(tictactoe.java:12)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:662)
thats the one with the issue i think
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and have you done anything else with line 12?
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . and have you done anything else with line 12?

im not sure what there is to do? i changed the name of the image to something more simple? should i try a different image?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying to run your applet from an IDE, aren't you? Because line 152 of java.awt.Applet says which means that "stub" is null. That field is set by calling setStub on the Applet instance, something that should automatically be done when the applet is called from a browser or the appletviewer application.
 
Wim Vanni
Ranch Hand
Posts: 96
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried the code in an Eclipse project. (Had to get some images of my own for this.)


I moved the images to a specific folder in the project and had them loaded from there. This solved the 'not initialized' error. There were some more things to tackle before I could enjoy a game of tic-tac-toe though ;-)

If you want hints:
  • iCount starts at 0; it never changes
  • the 'win label' isn't showing dynamically the winning letter (move the assignment)
  • the actionlistener is working on wrong JButtons in several places


  • If you haven't already, take a look at Oracle's (Sun's) page on creating applets. The tutorial and example there could keep you from falling into some other pitfalls.

    Have fun!
    Wim
     
    Anthony Pena
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    so after alot of work thats what i got, it works, im happy lol
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20511
    54
    Chrome Eclipse IDE Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, that of course make sense. You previously initialized the images when the applet was created. At that time it did not have an AppletStub yet. At the time init() is called it does, so getCodeBase() will work at that point.

    So the lesson learned (also for me!): do not call any methods of java.awt.Applet (or javax.swing.JApplet) until init() has been called, as you can't guarantee that they will work before that time.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic