Passing In Variable From One Class To Another?

Aug 16, 2014

It just wont work... Everytime I try to pass a variable into a class it sets it to 0. What to do.

The variable should be '2', but it prints out as 0!?

Java Code: package Zoo;
import java.util.Random;
public class Animal {
Random random = new Random();
public void Eat(){
System.out.println("The animal starts to eat.");

[Code]...

The method bash, needs the variable 'playerTwo' to be 2, to carry on. But it prints out 0.

Int the animal constructor, I passed in the variable from another class, and it prints out 2 in the constructor like it should. But outside of the constructor it is equal to 0.

View Replies


ADVERTISEMENT

Error Passing Value Of A Variable From One Class To Main Method Of Another Class

Jan 8, 2014

I've 3 classes.

1. Circle
2. GetInputFromUser
3. testCircle

package ABC;
public class Circle {
private double radius;
public double getRadius() {
return radius;

[Code] .....

In the testCircle class, in the line: getRadius = ui1.GetInput();

It's showing the error: The method GetInput(float) in the type GetInputFromUser is not applicable for the arguments ()

And when I do: getRadius = ui1.GetInput(rad);

It's showing the error: rad cannot be resolved

View Replies View Related

Passing Variable From Class A To B To Do Calculation But Getting Error?

Mar 23, 2015

I'm getting this error, I definitely know that is my error trying to pass method/variable because when I commented received part of my code it ran and worked.

I get this error

at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Here are my 2 classes

Java Code:

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Passing Data Through Globally Created Table Variable?

Jun 14, 2014

I have created a DefaultTableModel tablel as a Global variable. The table is then created and attached to a Grid bag layout. Then I want to call the table again in another method to add rows of data into it. Hopefully that makes sense.

So the addrow for the table is located in the final method private class CalcButtonListener implements ActionListener

When I debug the code, deftablemodel variable is carrying NULL data.

Also to make things even more complicated, The actual headers for the table aren't showing up,... not entirely sure why though.

import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import javax.swing.JMenuBar;

[code]....

View Replies View Related

Passing Value From Sub Class To Super Class

Oct 19, 2014

So the first thing we did was make a program called memory calculator. now we are supposed to make a sub class that modifies the program to also do exponents and natural logs. Then to make a new class that acts as the driver for what is now called the scientific memory calculator which includes the exponent and log. I have watched this through debug and im a little confused. first off it creates a variable for currentValue in current and in sci which are my instances, which i dont think is what i really want to do, I am unsure how to get the things in the Scientificmemcalc to do stuff without calling them directly. so when i try to do the power feature it does everything correct but it sets the sci current value to the correct answer, and now i cant figure out how to pass it on to the current current value.

This is the memory calculator :

public class MemoryCalculator {
public static void main(String[] args) {
MemoryCalculator current = new MemoryCalculator();//creates an instance
int choice = 0;
do{//main loop
System.out.println("Current value: " + current.getCurrentValue() + "

[Code] .....

View Replies View Related

Value Not Passing From Second Class?

Nov 8, 2014

I am trying to pass value from class to another using net beans but it seems that I am doing something wrong.

find my basic code at sourceforge.net/projects/val1toval2/files/SRC/

I would like to have both VAL1 and VAL2 having same value in Jlabel.

//Main.java 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Second SEC = new Second();
l_dis.setText(String.valueOf(getInput()));
l_tot.setText(String.valueOf(SEC.getTotal()));

[code]...

View Replies View Related

Passing Value From JTextField In One Class To Another

Apr 6, 2015

I enter a value in the text box in one class and want to pass that value to another class in order to compare it and color the cell in a table. In the first class I have

package cege.ui;
 import cege.controller.HtmlController;
import cege.controller.ScreenCaptureController;
...
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

[Code] .....

How to pass the threshhold from the first to the second class?

View Replies View Related

Getting Values From One Class To Another Without Passing Into It

Feb 19, 2015

I am trying to get the username and password for one class and send them to another. However I cannot send them in the constructor because a database class is getting them and I need that constructor blank. However if I do not pass the class in the constructor I get a NULL error, even better when I do pass it in I seem to get a thread lock. I do try to do my own housekeeping and close the one frame before I open a new one but it doesn't work. I will do my best to show the design of the program.

program starts
Java Code: public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// connects to database and generates a GUI
new UserPromptScreen();

[code]....

I have to pass in UserPromptScreen in for it to work. This will cause me issues later with my design.

View Replies View Related

JSF :: Passing Backing Bean To Another Class

Mar 7, 2014

I am new to JSF and was trying to find an example.

My question is in the processPage() method... how do i pass the managed bean to the ProjectDAO insert statement?

Faces-config.xml

<managed-bean>
<managed-bean-name>projectBean_backing</managed-bean-name>
<managed-bean-class>com.att.ped.backing.Project</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

[Code] .....

View Replies View Related

Passing JComboBox To Another Class - JAVA GUI

Nov 15, 2014

Java Code:

createConnection();
try{
String str = "select * from Stocks";
stmt = conn.prepareStatement(str);
ResultSet rs = stmt.executeQuery();
while(rs.next())
jcbStock.addItem(rs.getString("stockID"));
}catch(Exception e){
e.printStackTrace();
} mh_sh_highlight_all('java');

I want to put this code in a class then call it in another class , but i do not how to do that. This code is working fine in my main class , but i want to write this code to another class for call it in main class.

View Replies View Related

Reading Input From JTextField And Passing It To Another Class

Sep 7, 2014

I am having trouble taking user input and passing it to another class as a variable. If I assign the value explicitly (see line 59), it works just fine. What I want to do though, is assign the input from inputField to the variable inputVariable. I tried using:

inputVariable = inputField.getText();

This does not work. Regardless of what is typed into inputField, the output I get is just:

"The user typed "

package example;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

[Code] .....

View Replies View Related

Passing Parameter From Object Of Class B To Object Of Class C By Use Of Class A?

Dec 13, 2014

Assuming that we have two classes B and C which inherit from class A. What is the best way to pass a parameter from an object of class B to an object of class C by the use of class A without using static variable and without defining a get function in B?

View Replies View Related

Using A Variable From Main Class In Another Class

May 31, 2014

I have a program with 4 classes, all of them in the same package, one of them is the Main class, and in that class I declared a variable named "port" of type int. One of the 3 another ones is the class Connection class, which it requires the port variable. I want to use this variable in the Connection class. How can I do it?Both classes are shown below:

Main.java
package server;
/* Imports */
/* Another variables */
int port; /* <-- IS THIS ONE */

[code]....

View Replies View Related

Static Variable Within A Class?

Dec 8, 2014

I have a class Tree in which all the methods to build a tree are in place. But however I would want variable of by Tree which is pointing to the last node being added to the tree.

So basically every time you keep adding a node to the tree you tail pointer gets updated to point to the last node. I have this so far.

public class NonEmptyTree implements Tree {
private Tree left;
private int data;
private int leftleafCount;
private int rightleafCount;
private Tree right;
private Tree tail; // This variable must be shared by all the object. There needs to just one tail pointer to the tree.
public Tree insert( data ) {
tail = // gets updated every time when new node gets added.

View Replies View Related

How To Transfer The Value Of A Variable From One Class To Another

Mar 12, 2015

I am developing a program that seems to need to transfer the value of a variable from one class to another. However, neither of the classes is the main class.

This simplified code should make my problem clear:

This is my main class where I create runone from Class1 and runtwo from Class2:

public class MainClass {
public static void main(String[] args) { 
Class1 runone = new Class1();
Class2 runtwo = new Class2(); 

}

This is Class1 where I set up x and give it the value 20. I also have a getter-method to be able to access the value of x from other classes.

public class Class1 {
int x = 20;
public int getX(){
return x;
}}

This is Class2, where I try to access the value of x that was set up in runone.

public class Class2 {
int y;
public Class2() {
y = runone.getX + 10;
System.out.println(y);
}
}

Unfortunately, I get the error message "runone cannot be resolved to a variable."

What do I do to be able to access the variable x (that is set up in Class1) from Class2?

View Replies View Related

How To Implement A Variable Into Another Class

Apr 13, 2015

I would like to implement a variable in a class that is used in another class, how can I do that?

I have a Mall class and a Customer class I would like to associate the position of the customer that is in the Mall class and also implement the same in the Customer class.

Here is a small part of the code without all the methods.

//the two objects
Mall m = new Mall("RandomMall",50,30);
Customer c1= new Customer("Jack",1240);
//the first part of the mall class without the methods
class Mall{
private String name;
private int width,length;
String[][]grid = new String[width][length];

[code]...

View Replies View Related

Pass A Variable From One Class To Another

Jan 5, 2014

As the title says, how can I pass a value from one variable to another class?

Example:

So here I'm switching one frame to another, called "redigeraProjekt". Now in this class, I want the value from .getSelectedIndex() pass over to that class. I've tried to use the variable "valIListan" but it cannot find it. Probably because it's "private" (?)

valIListan = listaAllaSpelProjekt.getSelectedIndex();
redigeraProjekt npj = new redigeraProjekt(); // "switching" to another frame
npj.setVisible(true);
this.setVisible(false);

I hope you understand what I'm saying

View Replies View Related

Each Class Contain Private Variable

Sep 22, 2014

I'm working on a project that contains multiple classes. Each class contains and must contain only PRIVATE variables. Here's my issue. When my test code calls for a new instance of "StudentClass" as so:

StudentClass studentClass = new StudentClass(offeredClass.getClassIdNumber(),
offeredClass.getClassName(),
offeredClass.getClassroom());

The corresponding constructor won't let me initialize it's variables because they are declared private within another class, as shown here:

StudentClass (float classIdNumber, String className, Classroom room){
this.classIdNumber = classIdNumber;
this.className = className;
this.room = room;
}

When getClassName, getClassroom, and getClassIdNumber are passed to a toString() method elsewhere in my test code. the output is returned just fine. When passed through the StudentClass, I'm getting Null across the board.

View Replies View Related

Get The Value Of A Variable Stored In Another Class Of Another Package?

Nov 12, 2014

Source packages

1.First package : CarDealer.java (JFrame form)

2. Second package : Vehicle.java (JFrame form)

- The user runs CarDealer JFrame in which appears a JTexField in which he types in some characters (pretending he types some data about selling licence).
- The user clicks on the JButton "Save" that stores the characters typed in inside a String variable called strLicence.
- The user clicks on the JButton "Open 'Vehicle Form'" that opens the Vehicle JFrame.
- Finally, in the Vehicle JFrame I want to write a code that can see/use the value of the String variable called strLicence.

In other words, for example, I want to store the value of strLicence in a new variable declared in the Vehicle class.

(Actually I'm working with a long and elaborate project for the dissertation of my bachelor's degree, so, for cutting to the chase, I simplified it by creating an essential JFrame project.)

I used netbeans

1. CarDealer.java

package Firstpackage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;

[code]....

I also attach the netbeans project in a zip file ....

View Replies View Related

Sandwich Class Static Variable

Apr 14, 2015

Sandwich class. I have thus far completed creating a sandwich class with a seperate sandwich Tester class to run with it. (this is according to the assignment). Now I must create Static variables for the sandwich class:

Add two static variables to the Sandwich class to count how many sandwiches are sold and how many slices of tomato are used. Initialize each to 0.Where do you add code to increment the sandwich counter? Determine this and then add code.

public class Sandwich
{
static int numOfSold = 0;
static int slicesUsed = 0;
private String meat;
private int numOfSlicesOfTomato;
private boolean lettuce;

[code]....

View Replies View Related

Class Cannot See Its Own Protected Variable From Another Package

Jan 30, 2014

how access levels work from subclasses and other packages, and I have discovered that a class cannot see it's own protected variable from another package which I thought it would be. I know that it says in the java docs "The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package." but I thought that would also include it's own class.

Java Code:

package food;
import food.fruit.*;
public class Food {
protected int protecte = 5;
private int privat = 5;
protected void method(){

[Code] ....

View Replies View Related

Cannot Access Variable Form Another Class

Jul 24, 2014

why I cant access a variable from another class, in particular why in the main class cant i assign " quiz1 = getQuizOne();" ? It constantly giving me error.

Java Code:

import java.util.Scanner;
public class Grade {
private int quiz1, quiz2, midtermExam, finalExam = 0;
public static void main(String[] args)
{
Student John = new Student();
John.StudentData();

[code]....

View Replies View Related

Variable From Superclass Using In JFrame Class?

Oct 9, 2014

I would like to pass a variable I have in my main to my JFrame. In my main I calculate which pictures I should show in my JFrame. Is it possible to make objects of these pictures in my main class and use them in my JFrame? something like my code below.

public class JFrameTesting {
 public static void main(String[] args) {
Image ImageVariable = Toolkit.getDefaultToolkit().getImage("C:1.jpg");
 MyJFrame f = new MyJFrame();
f.setTitle("Drawing Graphics in Frames");

[code]....

View Replies View Related

How To Inject Variable That Is Instantiated In One Class Into Another

Mar 19, 2014

Let's say I have a Junit4 class FooTest.java and variable token. At some point in class the token gets instantiated.

Java Code:

public class Foo {
private String token;
@Before
public void setUp() {
// serious is steps to get the application to a certain state where token can get extracted
....
token = extractToken();
}
} mh_sh_highlight_all('java');

Now I have another class User.java where I need to use this token. Can I inject this token somehow into that class? There is no relation between Foo and User. I can't use Provider method in the configure file because extraction of the token depends on certain system's state and can't be called anywhere anytime.

View Replies View Related

Difference Between Instance And Class Variable

Mar 25, 2014

I'm having trouble to fully understand the difference between instance and class variables.

I though it was all about the static, i.e:

int age; // instance variable
static int age; // class variable

What's behind this?

View Replies View Related

All Instances Of Same Class Change Each Other Private Variable

May 2, 2015

I have a class named Base and a private variable named _hopcount i have 10 instances of class base i use _hopcount as creteria to some if but other instances edit _hopcount so i want to prevent _hopcount edit by other instances; I want to have private variable which other instances of same class can't modify it.

public class Base extends TypedAtomicActor {
private int _hopcount = 0;
if(_hopcount <= 3) {
some code;
}
public function() {
_hopCount += 1;
}
}

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved