3 Replies - 156 Views - Last Post: 12 July 2014 - 04:01 AM Rate Topic: -----

#1 RedneckSOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-July 14

Having Trouble with Java Applet when adding a subroutine

Posted 11 July 2014 - 11:03 PM

Hopefully I worded my question in a decent enough manner. So I am a beginner when it comes to Coding in general, but I generally understand the basic's. I am trying to design a generic RPG game to assist with my learning of Java.

The issue I am having right now is in my Class I have a subroutine (think that is the correct term) that is basically set up to hold a series of Print Statements. I am really just trying to get some Values that are stored within the Applet, have them assigned to their correct variables and then returned in the print statement. Yet, when I run the applet it just pops up the Applet blank and gives me a long list of errors and I really don't understand when they mean.

Here is the code: The Class

public class CharacterSheet {
	final String NL = System.getProperty("line.separator");
	public String characterName;	
	int playerStr;
	int playerCha;
	int chaMod;
	int strMod;
	
	public CharacterSheet(int newPlayerStr, int newPlayerCha){
		playerCha = newPlayerCha;
		playerStr = newPlayerStr;
		statStrMod();
		statChaMod();
		
	}
	public void setPlayerStr(int newPlayerStr){
		playerStr = newPlayerStr;
	}
	public void setPlayerCha(int newPlayerCha){
		playerCha = newPlayerCha;
	}
	public void setChaMod(int newChaMod){
		chaMod = newChaMod;
	}
	public void setStrMod(int newStrMod){
		strMod = newStrMod;
	}
	public int getPlayerStr(){
		return playerStr;
	}
	public int getPlayerCha(){
		return playerCha;
	}
	public int getChaMod(){
		return chaMod;
	}
	public int getStrMod(){
		return strMod;
	}
	
	public void statChaMod(){
		if(getPlayerCha() <= 0){
			chaMod=-4;
		}else if(getPlayerCha()<=3){
			chaMod=-3;
		}else if(getPlayerCha()<=6){
			chaMod=-2;
		}else if(getPlayerCha()<=9){
			chaMod=-1;
		}else if(getPlayerCha()<=10){
			chaMod=0;
		}else if(getPlayerCha()<=13){
			chaMod=1;
		}else if(getPlayerCha()<=16){
			chaMod=2;
		}else if(getPlayerCha()<=19){
			chaMod=3;
		}else if(getPlayerCha()>=20){
			chaMod=4;
		}
	}
	public void statStrMod(){
		if(getPlayerStr() <= 0){
			strMod=-4;
		}else if(getPlayerStr()<=3){
			strMod=-3;
		}else if(getPlayerStr()<=6){
			strMod=-2;
		}else if(getPlayerStr()<=9){
			strMod=-1;
		}else if(getPlayerStr()<=10){
			strMod=0;
		}else if(getPlayerStr()<=13){
			strMod=1;
		}else if(getPlayerStr()<=16){
			strMod=2;
		}else if(getPlayerStr()<=19){
			strMod=3;
		}else if(getPlayerStr()>=20){
			strMod=4;
		}
	}
	
	public void saying(){ 
		System.out.printf("Player's Strength: %s", playerStr);
		System.out.print(NL);
		System.out.print(strMod);
		System.out.print(NL);
		System.out.printf("Player's Chirisma: %s", playerCha);
		System.out.print(NL);
		System.out.print(chaMod);
		}

}



And the Applet:
import java.awt.*;
import javax.swing.*;

public class TFApplet extends JApplet{

	CharacterSheet test = new CharacterSheet(17,12);
	
	public static CharacterSheet charactersheet;
	
	
	public void paint(Graphics g){
		super.paint(g);
		String deploy = test.saying;
		g.drawString(deploy, 25,25);
	}
	
	
}



And the error messages:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException: String is null
	at sun.java2d.SunGraphics2D.drawString(Unknown Source)
	at TFApplet.paint(TFApplet.java:15)
	at javax.swing.RepaintManager$3.run(Unknown Source)
	at javax.swing.RepaintManager$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.access$1000(Unknown Source)
	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$200(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException: String is null
	at sun.java2d.SunGraphics2D.drawString(Unknown Source)
	at TFApplet.paint(TFApplet.java:15)
	at javax.swing.RepaintManager$3.run(Unknown Source)
	at javax.swing.RepaintManager$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.access$1000(Unknown Source)
	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$200(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)




If anyone can help me out that would be great. And I hate to sound picky about the answers but could they be in terms that are somewhat "noob friendly".

Is This A Good Question/Topic? 0
  • +

Replies To: Having Trouble with Java Applet when adding a subroutine

#2 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 74
  • View blog
  • Posts: 171
  • Joined: 19-October 13

Re: Having Trouble with Java Applet when adding a subroutine

Posted 12 July 2014 - 12:51 AM

The error message says "String is null".

Does this line really compile?
String deploy = test.saying;

I thought you would have to have parenthesis after the method name to call it.
String deploy = test.saying();

But that will not work because the function has return type void.
Was This Post Helpful? 0
  • +
  • -

#3 RedneckSOD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-July 14

Re: Having Trouble with Java Applet when adding a subroutine

Posted 12 July 2014 - 02:31 AM

View PostPeter O, on 12 July 2014 - 12:51 AM, said:

The error message says "String is null".

Does this line really compile?
String deploy = test.saying;

I thought you would have to have parenthesis after the method name to call it.
String deploy = test.saying();

But that will not work because the function has return type void.


I'm still pretty new to coding in general so if I made some stupid mistakes i'm sorry. I am not quite sure how to go about restructuring the code to make it work in the manner I want it too. Do you have any suggestions that will help me get on course with this as far as tutorials are concerned?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,146
  • Joined: 20-September 08

Re: Having Trouble with Java Applet when adding a subroutine

Posted 12 July 2014 - 04:01 AM

    public String saying() {
        return String.format("Player's Strength: %s%s%s%s, Player's Charisma: %s%s%s",
            playerStr, NL, strMod, NL, playerCha, NL, chaMod);
    }


is what you need. Call it per Peter O's last code line
To be more compliant with Java standards, that method should probably be called toString

This post has been edited by g00se: 12 July 2014 - 04:05 AM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1