Passing Object Created In One Method To Another?

Oct 29, 2014

I am working on an independent project it is a simple little text based rpg that will run in a counsel window. I have an object for Character that is creating during a CreateCharacter method. I want the play to be able to enter a character that will open up a menu that displays things like the name and health and stuff of the character from the object created in CreateCharacter, but because I have it in a different class I don't know how to reference the object made in CreateCharacter.

I have it in 6 files

Character --- Object with getters/setters for things like name, age, race, class, ect
MainMenu --- Displays title and promts for new game and quit
CreateCharacter --- Walks through and sets all values in Character
Stats --- Keeps the players stats (health, attack, ect) in an array
Intro --- Beginning demo thing (not really important for this question)
Menu --- Displays all current user stats (Having issues with this one)

Example I have this in Menu

System.out.println("Name: " + ????.getName());

View Replies


ADVERTISEMENT

Which Method Is Used While Passing Or Returning A Object From The Native Method

Mar 5, 2014

Which method is used while passing or returning a java object from the native method?

View Replies View Related

Passing Object From JSP Page Through JSTL Into Controller Method

May 12, 2015

I need to pass advanced object into controller from JSP page, but I always get null result.It's a controller method:

Java Code:

@RequestMapping(value="admin-user-edit", method=RequestMethod.POST)
public ModelAndView editUser(@ModelAttribute(value="user") UsersEntity user)
{
if (null == user)System.out.println("User is null");
else
System.out.println("User name = " + user.getName() + " | Users id = " + user.getId());
ModelAndView view = new ModelAndView();
return view;
} mh_sh_highlight_all('java');

And this is a JSP page snippet. I need to choose some user from user list and pass it to controller.

XML Code:

<c:forEach var="user" items="${user_list}">
<tr>
<td><c:out value="${user.id}" /></td>
<td><c:out value="${user.name}" /></td>
<td><c:out value="${user.login}" /></td>
<td><c:out value="${user.status}" /></td>

[code]...

I tried to pass it through HttpServletRequest argument but all the same.

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 Object Argument To A Method - Unable To Call Methods On Argument

Feb 7, 2015

I am trying to pass an object of type Product p to my editProduct method, however trying to call p.getName(); doesn't work and throws a NullPointerException. The same kind of thing works for my displayRecord method (in a different class) and I can call .getName() on Product p, also passed as an argument to that method. Below is my editProduct class. The NullPointerExcepion is being thrown at line 61 (i.e., nameField.setText(p.getName());).

I don't know if I explained right, so here's a line thing of how the classes relate:

Search >>>(field.getText())>>> displayRecord(Product p) >>>editProduct(p)>>> EditProduct

And as a side note: adding the line p = new Product(); fixes it and successfully runs the class (including the Save and Quit parts) but obviously I want it to specifically refer to the Product I pass to the method.

Java Code:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;

[Code] ....

I'm asking a question because I don't understand how Product p could possibly be null, because the argument is passed through my DisplayRecord class, which also takes a Product p argument and works. In that class, I have declared Product prod = p; and prod is what I am passing to editProduct.

View Replies View Related

JSP :: Can Use Object Which Is Created In Servlet?

Dec 7, 2005

How should I access object, which I have created in servlet? Servlet handles the requests(controller) and forwards to requested jsp page.Some of the jsp pages need EJB objects. When i create an ejb object in servlet and then forward the request to jsp, how can i access the object in jsp ? This should be MVC - based application, like JSP-Servlet-EJB.

View Replies View Related

How To Reference Object Created In One Class From Another

Oct 30, 2014

I have started working on a little project in my free time. It is just a simple text rpg that runs in a counsel window. I have 5 files each file contains 1 class.

public class SomnusCharacter {
private String gender = "";
private int age = 0;
private String race = "";
private int level = 0;
private int xp = 0;

[Code] ....

The chain of events right now is:

1. MainMenu is run
2. If user inputs n CreateCharactor is run
3. User inputs name, age, ect in SomnusCharacter object made in CreateCharacter
4. Intro (just rough demo for testing purposes) is run
5. If user inputs m Menu is run
6. Menu calls and prints out all the information from the object made in CreateCharacter

Step 6 is where I am having my problems. How can I reference (lets say the SomnusCharacter object made is called player) player from my Menu class? I know that if I made a new character that it would just create another SomunsCharacter object with the default values again.

View Replies View Related

Object Created Inside Switch

Mar 2, 2014

I have create as short example which contains two classes Cat and Dog followed by a switch statement in the main method.

Class Cat

public class Cat {
private String name;
//Setter
public void setName(String pName)
{name = pName;}
//Getter
public String getName()
{return name;}
// Constructor
public Cat(String catName)
{name = catName;}

[code]....

My Issue is that after creating the objects within the switch statement, is there a way to return the objects back to the main method once created within the switch ?

View Replies View Related

Accessing Object Created In Another Class

Mar 10, 2014

I have a situation where I have 2 classes and an array of objects which are causing me trouble.

The object type is one I have created - it is made from a class which is neither of the 2 classes I previously mentioned.

The array is created and occupied in Class1 and the problem arises when I try to reference one of the element from Class2.

At first I forgot the the array would be local to Class1.main so I made the array a global variable using:

Java Code: public MyObjectType[] myArray; mh_sh_highlight_all('java');
Then I tried accessing an element (2) from Class2 using:

Java Code: Class1.myArray[2] mh_sh_highlight_all('java');
However I get errors saying that I can't access the static variable from a non-static context.

I understand a little bit about static and non-static objects/methods but don't know how to fix this. Do I need to include "static" in the array declaration?

View Replies View Related

Servlets :: When HTTP Session Object Created In Web Application

Jul 17, 2014

When does HTTP Session object is created in web application. Suppose I have a website. Home page of website is HTTP page which contains details of company and link to Login page.

Consider below mentioned user journey as scenario:

a. user arrives at home page of website
b. user click on Login page
c. user fill in login details on login page and click on Submit
d. user is successfully authenticated and authorized from back end
e. User specific page is shown
f. user click on logout link
g. user is successfully logged out from website
h. user is redirected to home page
i. user closes browser

In the above mentioned user journey,

a. at which step does HTTP session starts (means at which steps does HTTP Session object is created ? )
b. at which step does HTTP session ends ?

In case required, assume tech stack to be Java 7, Servlet 2.5, JSP, Tomcat 7, Apache web server (for static web contents).....

View Replies View Related

Created Tree Lemur Object - Returning Null Consistently From Methods

May 24, 2015

The question pretty much says it all, but I tasked myself with creating a program about lemurs. There are multiple class files in this program. In the below code snippet, I have my TreeLemur.class which extends to the Lemur.class which extends to the Mammal.class. However, when I create a Tree Lemur object in the main program, it is returning null consistently from certain methods. What am I doing wrong here?

TreeLemur.class :

public class TreeLemur extends Lemur {
private String groupSize;
private String diet;
private String fur;
public void setGroupSize() {
groupSize = "
Group Size: Large";
}

[Code]...

As of yet, I'm just trying to get Tree Lemur working properly to continue with creating the other if-branches within the main program.

View Replies View Related

How Many Objects Created With New String Method

May 22, 2014

I am little confused about String creation in java.

Doubt 1: How String objects assigned to Pool area:

1. String s="in pool";
2. String s1= new String("not in pool");

How many objects created in statement 1 and 2. According to recent discussion with my colleague, one object created in String pool in case 1. And in case 2, two objects are created, one as literal goes to String pool and other with new() opr goes to Heap.

If above is correct, Ain't we wasting double memory for same object ? Really need clear understanding on this

Doubt 2: How does intern() work: Please see if my below explanation is correct

1. If String literal is already present in String pool , and i create a same string with new operator, reference to object is changed to pool area.

2. If String object is created using new operator and intern is called on it. If same string object is not present in the String Pool, Its moved to String pool and reference to this in Pool is returned.

View Replies View Related

Passing Object As Parameter?

Aug 8, 2014

i want to pass an object of type Software to assign it to a computer from Computer class...

i should note that computer and software are arrays of objects thats the variable and method to set software in Computer class

private Software[] software;
public void setSoftware(Software software,int index){
this.software[index]=software;}

note: the user choses the a computer from a list and a software as will

for example the program will show the user 2 computers

0 for computer: apple, Model: mac mini, Speed: 2.8

1 for computer: sony, Model: vaio, Speed: 2.2

the user enters the index he wants then the program will show a list of software to add to the computer selected

the error I'm having is run time error Exception in thread "main" java.lang.NullPointerException and it points to these 2 lines

1.comp[Cch].setSoftware(software,Sch);

2. the method setSoftware

every thing is running correctly but this step above

Cch= the chosen computer index

Sch= the chosen Software index

why am i getting an error and how to fix it?

View Replies View Related

Servlets :: Why Query String Is Not Getting Created In DoPost Method

Dec 9, 2014

Why query string is not getting created in doPost method of HttpServlet?Is this reason is enough - it is supplying data in html body.

View Replies View Related

Passing A Graphics Object To 3 Other Classes?

Oct 5, 2014

Basically I am making a paddleball game, like i'm sure everyone does in learning Java. I'm supposed to use different classes for each component, i.e. one for the ball, one for the paddle, and one for the display, then finally one as a 'controller' to implement mouselistener and stuff.

However, I can't quite grasp how to implement the paintComponent method. I know I can only have it in one class extended from JPanel, and I have the syntax for creating an object which I understand is something like this:

public void paintComponent(Graphics g){ //this is the rectangle my game will be played on,
super.paintComponent(g); //a gray background to define boundaries for the ball
g.setColor(Color.GRAY);
g.drawRect(0, 0, Frame.getHeight(), Frame.getWidth());

However what I don't understand is, how do I then pass this graphics object to the ball and paddle to let them draw themselves? I found something that described it like this here

class GamePanel extends JPanel {
Entity e=new Entity;
@Override
protected paintComponent(Graphics g) {
super.paintComponent(g);

[Code] .....

What I don't get is, if I use this, where would I put the drawRect and stuff to make the other shapes I need? in their class, under the entity.Draw(g) method? or in the display class where it calls the graphic object in the first place?

Last, how can I have my controller class refresh the displays of each of these with the timer I have implemented? Is there a simple way to call one refresh command and have it refresh the drawing of both the paddle and ball simultaneously, or would I need to call a separate refresh command for each object?

View Replies View Related

Passing Object As Parameter - Combination Values Are All Zero

Mar 8, 2015

I have an object that has an instance of another object class as its parameter :

CombinationLock oneHundred = new CombinationLock(28,17,39);
Locker Mickey = new Locker(100, "Mickey", 3, oneHundred);

This is for a locker, which has a combination assigned to the student. Within the locker class I have the following constructor:

public Locker(int locker, String student, int numberOfBooks, CombinationLock combo) {
this.locker = locker;
this.combo = combo;
this.student = student;
this.numberOfBooks = numberOfBooks;
}

combo is the private CombinationLocker object I created within the Locker class. Do I need to pass the combo object on to the CombinationLock class? For reason, I do not comprehend, the combination password from the main class is not passing through to the CombinationLock class, and the combination values are all zero.

View Replies View Related

JSF :: Passing Parameters From Normal List Object Without Using DataModel

Oct 9, 2014

How to use the id parameter in my documents entity to download documents from a list of documents. Normally I use ListDataModel and the getRowData method. I would like to know how to achieve the same thing using an ordinary List object.

My list of documents is called List<CountryDocs> selectedDocs;

<h:form>
<p:dataTable value="#{countryDocBean.selectedDocs}" var="docs">
<p:commandLink id="download" value="Download" ajax="false">
<p:fileDownload value="#{countryDocBean.downloadedFile}"
contentDisposition="attachment"/>

[Code] ....

Clicking on the download link calls the following method in my managed bean:

@ManagedBean(name = "countryDocBean")
@SessionScoped
public class CountryDocBean {
private List<CountryDocs> selectedDocs;
public StreamedContent getDownloadedFile() {

[Code] ....

Debugging shows the value for the id is 0 and this results in a NullPointerException. I've tried several methods for grabbing the document id in my backing bean, but no luck yet. I also read about the the ViewParams and ViewAction method but they caused validation errors to do with the <f:metadata> tags. I don't know how to obtain this value using a normal List object.

View Replies View Related

Passing A Value Into A Method

May 8, 2014

Does the following code pass a value into a method?

/code

lnValue[x][y] = computelnValue();

View Replies View Related

Passing More Than One Array In A Method?

Nov 3, 2014

Can we pass more than one array in a method?

View Replies View Related

Passing Information To A Method Or Constructor

Feb 28, 2014

Passing Information to a Method or a Constructor

They show a line of code that goes like this:

public Polygon polygonFrom(Point[] corners) {
// method body goes here
}

So from what I understand this is a constructor method for a Polygon object from the Polygon class. What I dont get is the name of the method polygonFrom()

Shouldn't a constructor for a Polygon just have the same name as the class? Because from earlier examples in the tutorial it seems to me that this is what has been done

For example:

public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

View Replies View Related

Passing A Boolean Parameter From One Method To Other

Jan 30, 2015

Java SE Runtime Environment build 1.8.0..This is part of the code:

public static int addAddress (String[] number, boolean[] front, double[] total) {
int num = 0;
double ffee = 0;
/*boolean value = false;*/
 
[code]...

I have tried using the line of code commented out, /*boolean value = false;*/. However, another error is displayed. The compiler shows the following...

Inspection.java:33: error: incompatible types: boolean cannot be converted to boolean[]
front[num]= defineFront(num, value);
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output error...I know that boolean values are by default stored as false, once you create the array. However, I'm having trouble passing the variable to the method.

View Replies View Related

Passing A Method And Searching Array

Apr 22, 2014

How to pass the methods listed below.

public ArrayList<Person> findPerson(String searchFor)

^ this method is suppose to search an array and find whatever you search (numerical or alphabetical)

I have this

public ArrayList<Person> findPerson(String searchFor) {
Main.handleSearchPerson(keyboard, searchFor);
ArrayList<Person> matches = new ArrayList<>();
for (Person p : personList) {
boolean isAMatch = false;

[Code] .....

I am not sure if how i am searching the method is correct or how to pass that to another class to put it inside of a switch statement.

View Replies View Related

Passing A Method And Searching Array

Apr 22, 2014

How to pass the methods listed below.

public ArrayList<Person> findPerson(String searchFor)

^ this method is suppose to search an array and find whatever you search (numerical or alphabetical)

i have this

public ArrayList<Person> findPerson(String searchFor) {
Main.handleSearchPerson(keyboard, searchFor);
ArrayList<Person> matches = new ArrayList<>();
for (Person p : personList) {
boolean isAMatch = false;
if (p.getFirstName().equalsIgnoreCase(searchFor)) {
isAMatch = true;

[Code] ....

I am not sure if how i am searching the method is correct or how to pass that to another class to put it inside of a switch statement.

View Replies View Related

Passing Java Bean Name In A Method

Jun 13, 2014

Here I am taking response data from JSON using jackson API.I found a feature like using Jackson the properties can be set to bean properties.
 
private static String readUrl(String urlString) throws Exception {       
BufferedReader reader = null;       
try {           
URL url = new URL(urlString);           
reader = new BufferedReader(new InputStreamReader(url.openStream()));     

[Code] ....

I got the correct output here. But now I want to generalize my method into a utility class so that I can reuse the same method for setting response data directly to respective beans as given below:-
 
My question is how will I pass the bean object in my utility class?

public static Object getResponseData(String response,[b]String bean[/b]) throws Exception {       
ObjectMapper mapper = new ObjectMapper(); 
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);       
JsonNode root = mapper.readTree(response);

[Code] ....

View Replies View Related

Passing Unknown Type To A Method And Returning It

Feb 20, 2014

So I want to know how in Java you can pass a unkown type into a method (type can be an int, double, or a user defined object) and return that unkown type.

example of what I want:
Java Code:
public (unknowntype)[] method2 ((unknowntype)[])
//Process Data
//unknowntype.process();
return (unknowntype);
} mh_sh_highlight_all('java');

I know in C you can use void pointers and in c++ we have templates but I do not know how java handles this. Also I want to know if it is possible to call a method in the unknowntype.

View Replies View Related

Servlets :: How To Change URL To JSP Name After Passing To DoPost Method

Aug 12, 2014

I have a log in page accessed by: localhost/Security/Login.jsp

When I click a log in button, doPost() method is executed from Login.java and displays the contents from main.jsp.

However,its url on my browser is localhost/Security/Login.

Generally, the pattern is the address of Login servlet.

So if I click a button from main.jsp, error occurs because it is trying to locate a servlet from /Security/[Servlet name].

What I want is to change the url as I go to main.jsp to localhost/Home/main.jsp.

Is this possible?

View Replies View Related







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