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]);
...
passing variables/calling methods from other classesunable to call methods or pass variables from one class to another
Page 1 of 1
8 Replies - 1001 Views - Last Post: 03 December 2009 - 03:33 PM
#1
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?
Replies To: passing variables/calling methods from other classes
#2
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
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
#3
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()?
#4
Re: passing variables/calling methods from other classes
Posted 03 December 2009 - 02:10 PM
Fraggle, 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.
#5
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?
#6
Re: passing variables/calling methods from other classes
Posted 03 December 2009 - 02:22 PM
Fraggle, 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);
}
}
#7
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
#8
Re: passing variables/calling methods from other classes
Posted 03 December 2009 - 02:43 PM
#9
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...
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|