passing variables/calling methods from other classes

unable to call methods or pass variables from one class to another

Page 1 of 1

8 Replies - 1295 Views - Last Post: 03 December 2009 - 03:33 PM Rate Topic: -----

#1 Fraggle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 09

passing variables/calling methods from other classes

Posted 03 December 2009 - 01:35 PM

I've been googling like crazy and i get the gist of having static variables and using them in another class, but i repeatedly get errors saying that getParameter() cannot be static :/ So how do i take parameters from an html page and pass it to CardPanel?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;


public class CharProject extends JApplet
{
	public static String[] attrb = StringArray(getParameter("Skills"));	  
	public String[] profString = StringArray(getParameter("Professions"));
	public Integer skills = new Integer(getParameter("Skills"));
 	
 	public void init ()
 	{
		Container content_pane = getContentPane ();
		CardPanel card_panel = new CardPanel ();
		content_pane.add (card_panel);
 	}


 			String[] StringArray(String aString)
	 			{
					String[] splittArray = null;
					if (aString != null || !aString.equalsIgnoreCase(""))
						{
						 	splittArray = aString.split(",");
					   	}
					return splittArray;
				}
} 

class CardPanel extends JPanel
				   implements ActionListener
{
...
JLabel name1 = new JLabel();
name1.setText(profString[1]);
...



Is This A Good Question/Topic? 0
  • +

Replies To: passing variables/calling methods from other classes

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: passing variables/calling methods from other classes

Posted 03 December 2009 - 01:42 PM

The problem doesn't come from getParameter, it comes from the fact that you call StringArray to initialize a static variable. Static variables are initialized when the class is loaded; however, StringArray is a instance method, so you need to instantiate an object of type CharProject to call this method.

Do you really need attrb to be static?


Edit: actually, the same applies for getParameter, so yeah, it does come from it too ;)

This post has been edited by EdwinNameless: 03 December 2009 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 Fraggle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 09

Re: passing variables/calling methods from other classes

Posted 03 December 2009 - 02:07 PM

I'm not entirely sure to be honest, as long as i can pass getParameter to CardPanel it doesn't matter too much. How would i go about getting that method to call from CardPanel and pass the getParamter()?
Was This Post Helpful? 0
  • +
  • -

#4 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: passing variables/calling methods from other classes

Posted 03 December 2009 - 02:10 PM

View PostFraggle, on 3 Dec, 2009 - 01:07 PM, said:

I'm not entirely sure to be honest, as long as i can pass getParameter to CardPanel it doesn't matter too much. How would i go about getting that method to call from CardPanel and pass the getParamter()?


Define setters in your CardPanel class, and in the init method of CharProject, set the values retrieved by getParameter.
Was This Post Helpful? 0
  • +
  • -

#5 Fraggle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 09

Re: passing variables/calling methods from other classes

Posted 03 December 2009 - 02:15 PM

so i should use the getParameter() and a setter in the int() method to set variables in the cardpanel class?
Was This Post Helpful? 0
  • +
  • -

#6 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: passing variables/calling methods from other classes

Posted 03 December 2009 - 02:22 PM

View PostFraggle, on 3 Dec, 2009 - 01:15 PM, said:

so i should use the getParameter() and a setter in the int() method to set variables in the cardpanel class?


Yes, for example:

import javax.swing.*;
import java.awt.*;

public class CharProject extends JApplet {

	public String[] attrb = stringArray(getParameter("Skills"));
	public String[] profString = stringArray(getParameter("Professions"));
	public Integer skills = new Integer(getParameter("Skills"));

	public void init() {
		Container content_pane = getContentPane();
		CardPanel card_panel = new CardPanel();
		card_panel.setName1Text(profString[1]);
		content_pane.add(card_panel);
	}

	String[] stringArray(String aString) {
	   // ...
	}
}

class CardPanel extends JPanel {
	JLabel name1 = new JLabel();

	public CardPanel() {
	}

	public void setName1Text(String name) {
		name1.setText(name);
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 Fraggle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 09

Re: passing variables/calling methods from other classes

Posted 03 December 2009 - 02:39 PM

i got alot of errors with that code, why is there a setname method inside the cardpanel method? :S
Was This Post Helpful? 0
  • +
  • -

#8 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: passing variables/calling methods from other classes

Posted 03 December 2009 - 02:43 PM

View PostFraggle, on 3 Dec, 2009 - 01:39 PM, said:

i got alot of errors with that code, why is there a setname method inside the cardpanel method? :S


It's the setter to set the text of the JLabel. It is called in the initialize method.
Was This Post Helpful? 0
  • +
  • -

#9 Fraggle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 09

Re: passing variables/calling methods from other classes

Posted 03 December 2009 - 03:33 PM

im just getting slapped with dozens of null pointer exceptions now... its so confusing...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1