Results 1 to 7 of 7
  1. #1
    Bitterguy is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Class "object cannot be resolves to a type"

    I am trying to get back into java using the "Head First Java Second Edition" book and im new to making separate classes (we didnt get o that point in my schools intro to java course, which was unfortunately there only one).
    Here is my code and a picture:


    Java Code:
    public class BlackjackStart {
    	public static void main(String[] args){
    		Blackjack game = new Blackjack();
    		game.playGame();
    	}
    }

    Java Code:
    public class Blackjack {
    	Player p1;
    	Player Dealer;
    	
    	public void playGame(){
    		p1 = new Player();
    	}
    }
    Java Code:
    public class Cardgen {
    	playerCard pc1;
    	playerCard pc2;
    	playerCard pc3;
    	playerCard pc4;
    	playerCard pc5;
    	dealerCard dc1;
    	dealerCard dc2;
    	dealerCard dc3;
    	dealerCard dc4;
    	dealerCard dc5;
    	
    		public void Deal(){
    			pc1 = new playerCard();
    			
    		}
    	
    }
    Class "object cannot be resolves to a type"-java-help.png

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Class "object cannot be resolves to a type"

    It's always best to post your complete error messages.

    You're declaring several variables as "DealerCard" variables. Where do you have this DealerCard class?

  3. #3
    Bitterguy is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Class "object cannot be resolves to a type"

    I don't have one. I didn't think I needed one since I am making dealerCard an object in the class Cardgen to use in the Deal method. I am also having the same problem with trying to use "Player" as an object in the Blackjack class. sorry like i said i am new to classes, my error log when i try to run the project:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Player cannot be resolved to a type
    Player cannot be resolved to a type
    Player cannot be resolved to a type
    Player cannot be resolved to a type

    at Blackjack.<init>(Blackjack.java:3)
    at BlackjackStart.main(BlackjackStart.java:4)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Class "object cannot be resolves to a type"

    You're trying to use classes before they've been created. I assume that the book has you create a Player class, right? I would create all the necessary supporting classes before trying to use any.

  5. #5
    Bitterguy is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Class "object cannot be resolves to a type"

    Thank you, that did it, now I have another question, how do i get an int from one method to affect another: i.e. in the provided code i want the confirm int from test.choice() to affect the confirm int in getCards.

    Players.java:
    Java Code:
    import java.util.Scanner;
    public class Players { // sets cards and determines choices
    	int confirm;
    	int choice;
    	public void choice(){
    		Scanner scanner = new Scanner(System.in);
    		
    		choice = 0;
    		confirm = 0;
    		
    		while (confirm == 0){
    		System.out.println("Would you like to: \n 1.Hit \n 2.Stay \n 3. Fold");
    		choice = scanner.nextInt();
    		
    		if (choice == 1){
    			System.out.println("You have chosen:\n 1. Hit \n Is this correct?\n(1 = yes 01 = no)");
    			confirm = scanner.nextInt();
    		}
    		}
    	}
    }
    Cardgen.java:
    Java Code:
    public class Cardgen {
    	int card1;
    	int card2;
    	int card3;
    	int card4;
    	int card5;
    	
    	
    	
    	public void getCards() {
    		Players test = new Players();
    		int confirm = 0;
    		card1 = (int) (Math.random() * 10);
    		System.out.println("your card was: " + card1);
    		test.choice();
    		if (confirm == 1) {
    			System.out.print("it worked");
    		}
    		card2 = 2;
    		card3 = 3;
    		card4 =4;
    		card5 = 5;
    	}
    }
    Outputs:
    your card was: 7
    Would you like to:
    1.Hit
    2.Stay
    3. Fold
    1
    You have chosen:
    1. Hit
    Is this correct?
    (1 = yes 01 = no)
    1

  6. #6
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Class "object cannot be resolves to a type"

    I don't understand your question. When you say "affect", do you mean that you want the integer confirm in getCards() to change to the value that it is assigned to in choice()?

    I assume that you have created an object of the class Cardgen. I'm not sure when you call the method getCards, or when you create the object of Cardgen, but you can use a setter to set the confirm in that object to the same as in the other.

    Either define your class Cardgen as follows,

    Java Code:
    public class Cardgen {
        int card1;
        int card2;
        int card3;
        int card4;
        int card5;
        int confirm = 0;
         
         
        public void getCards() {
            Players test = new Players();
    
            card1 = (int) (Math.random() * 10);
            System.out.println("your card was: " + card1);
            test.choice();
            if (confirm == 1) {
                System.out.print("it worked");
            }
            card2 = 2;
            card3 = 3;
            card4 =4;
            card5 = 5;
        }
    }
    and then use a setter

    Java Code:
    import java.util.Scanner;
    public class Players { // sets cards and determines choices
        int confirm;
        int choice;
        Cardgen cg;
    
        public void choice(){
            Scanner scanner = new Scanner(System.in);
             
            choice = 0;
            confirm = 0;
             
            while (confirm == 0){
            System.out.println("Would you like to: \n 1.Hit \n 2.Stay \n 3. Fold");
            choice = scanner.nextInt();
             
            if (choice == 1){
                System.out.println("You have chosen:\n 1. Hit \n Is this correct?\n(1 = yes 01 = no)");
                confirm = scanner.nextInt();
                cg.confirm = confirm;
            }
            }
        }
    }
    Note that this code is not going to work by just copying and pasting it. You need to create the object of the Cardgen class and set "cg" to it in the Player class. Somehow, I think it would be good for the objects to have references to each other.

    You can do it in many ways, and I'm sure this isn't the best way, but since I don't know when your methods are called, how they are used or what references they have to each other, it's hard to say what the best way is!

    Hope I understood your question, if not, please be more specific!

    Cheers,
    Z!
    Last edited by Zyril; 08-23-2012 at 12:14 AM.

  7. #7
    Bitterguy is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Class "object cannot be resolves to a type"

    Quote Originally Posted by Zyril View Post
    I don't understand your question. When you say "affect", do you mean that you want the integer confirm in getCards() to change to the value that it is assigned to in choice()?

    I assume that you have created an object of the class Cardgen. I'm not sure when you call the method getCards, or when you create the object of Cardgen, but you can use a setter to set the confirm in that object to the same as in the other.

    Either define your class Cardgen as follows,

    Java Code:
    public class Cardgen {
        int card1;
        int card2;
        int card3;
        int card4;
        int card5;
        int confirm = 0;
         
         
        public void getCards() {
            Players test = new Players();
    
            card1 = (int) (Math.random() * 10);
            System.out.println("your card was: " + card1);
            test.choice();
            if (confirm == 1) {
                System.out.print("it worked");
            }
            card2 = 2;
            card3 = 3;
            card4 =4;
            card5 = 5;
        }
    }
    and then use a setter

    Java Code:
    import java.util.Scanner;
    public class Players { // sets cards and determines choices
        int confirm;
        int choice;
        Cardgen cg;
    
        public void choice(){
            Scanner scanner = new Scanner(System.in);
             
            choice = 0;
            confirm = 0;
             
            while (confirm == 0){
            System.out.println("Would you like to: \n 1.Hit \n 2.Stay \n 3. Fold");
            choice = scanner.nextInt();
             
            if (choice == 1){
                System.out.println("You have chosen:\n 1. Hit \n Is this correct?\n(1 = yes 01 = no)");
                confirm = scanner.nextInt();
                cg.confirm = confirm;
            }
            }
        }
    }
    Note that this code is not going to work by just copying and pasting it. You need to create the object of the Cardgen class and set "cg" to it in the Player class. Somehow, I think it would be good for the objects to have references to each other.

    You can do it in many ways, and I'm sure this isn't the best way, but since I don't know when your methods are called, how they are used or what references they have to each other, it's hard to say what the best way is!

    Hope I understood your question, if not, please be more specific!

    Cheers,
    Z!
    all of my code is as followed:

    Java Code:
    public class GameStart { //starts the game
    	public static void main (String[] args){
    		Blackjack game = new Blackjack();
    		game.go();
    	}
    }
    Java Code:
    public class Blackjack { // runs main stuff
    	Cardgen pHand;
    	
    	public void go() {
    		Cardgen test = new Cardgen();
    		
    		
    		test.getCards();
    		System.out.println("test"); // to make sure this class runs
    		System.out.println(pHand); // for later testing
    	}
    }
    Java Code:
    import java.util.Scanner;
    public class Players { // sets cards and determines choices
    	int confirm; // needs to be affected by any calling of the "choice()" method
    	int choice;
    	Cardgen cg;
    	public void choice(){
    		Scanner scanner = new Scanner(System.in);
    		
    		choice = 0;
    		
    		
    		while (confirm == 0){
    		System.out.println("Would you like to: \n 1.Hit \n 2.Stay \n 3. Fold");
    		choice = scanner.nextInt();
    		
    		if (choice == 1){
    			System.out.println("You have chosen:\n 1. Hit \n Is this correct?\n(1 = yes 0 = no)");
    			confirm = scanner.nextInt();
    			cg.confirm = confirm;
    		}
    		}
    	}
    }
    Java Code:
    public class Cardgen { //generate card
    	int card1;
    	int card2;
    	int card3;
    	int card4;
    	int card5;
    	int confirm = 0;
    	
    	
    	public void getCards() {
    		Players test = new Players();
    		
    		card1 = (int) (Math.random() * 10); // will go 1-52 later
    		System.out.println("your card was: " + card1);
    		test.choice(); // choice method from "Players" class
    		if (confirm == 1) {
    			System.out.print("it worked"); // testing to see if confirm changed
    		}
    		card2 = 2;
    		card3 = 3;
    		card4 = 4;
    		card5 = 5;
    	}
    }
    turns out as followed:

    your card was: 2
    Would you like to:
    1.Hit
    2.Stay
    3. Fold
    1
    You have chosen:
    1. Hit
    Is this correct?
    (1 = yes 0 = no)
    1
    Exception in thread "main" java.lang.NullPointerException
    at Players.choice(Players.java:20)
    at Cardgen.getCards(Cardgen.java:16)
    at Blackjack.go(Blackjack.java:9)
    at GameStart.main(GameStart.java:5)

    Sorry about this whole thing, like i said first time dealing with separate classes and first project in a while, thanks

Similar Threads

  1. Replies: 11
    Last Post: 08-18-2011, 10:54 PM
  2. Replies: 4
    Last Post: 06-14-2011, 04:59 AM
  3. How to inherit "Object" class in Java
    By Ipsita in forum Advanced Java
    Replies: 5
    Last Post: 04-08-2011, 12:37 PM
  4. lock on ".class" object
    By rajinder5 in forum Threads and Synchronization
    Replies: 0
    Last Post: 10-11-2010, 04:38 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •