aspose file tools*
The moose likes Beginning Java and the fly likes tictactoe help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "tictactoe help" Watch "tictactoe help" New topic
Author

tictactoe help

Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
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

Joined: Aug 16, 2005
Posts: 14345
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
thank you!
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
how do you mean combine them?
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28


got rid of all my errors, but now its not running ? it says it didn initialize
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Check the Java Console in your system tray, there's probably an exception. Post the full stack trace here.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
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

Joined: Oct 13, 2005
Posts: 39828
    
  28
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

Joined: May 14, 2011
Posts: 28
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

Joined: Oct 13, 2005
Posts: 39828
    
  28
. . . and have you done anything else with line 12?
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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

Joined: Apr 06, 2011
Posts: 96

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

    Joined: May 14, 2011
    Posts: 28


    so after alot of work thats what i got, it works, im happy lol
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19760
        
      20

    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.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: tictactoe help