Modify Values In Fields In Serializable Object Using Objects Set Methods Java

Dec 10, 2014

This program is basically complete. It compiles and runs. It is a college course assignment that I pretty much completed but for the last part in which I'm suppose to change the values of all fields and display the modified values using a toString method. Modifying the values of the fields is where I am stuck. I don't think I need to create a new text data file to do this. The instructor only asked that all the values of fields be changed and this was the last part of the assignment so I don't think it involves creating additional ObjectOutputStream and ObjectInputStream objects. I'm getting a NullPointerException error on line 161.Here is the code. I'm also including the input data file.

//create program so that objects of class can be serialized, implements interface Serialiable
//create constructor with 4 parameters with accompanying get and set methods, Override toString method
//create text file with 5 records, create Scanner object,ObjectOutputStream, and ObjectInputStream
//create new ItemRecord object, change values of all fields in ItemRecord object using object's set methods
//modify ItemRecord object using toString method

[hightlight =Java]import java.io.Serializable;
public class ItemRecord implements Serializable

[Code] .....

This is the error message:

----jGRASP exec: java ItemRecordReport

Item Cost Quantity Description
Number
A100 $ 99.99 10 Canon PowerShot-135
A200 $149.99 50 Panasonic-Lumix T55
A300 $349.99 20 Nikon- D3200 DSRL
A400 $280.99 30 Sony- DSC-W800
A500 $ 97.99 20 Samsung- WB35F
Exception in thread "main" java.lang.NullPointerException
at ItemRecordReport.main(ItemRecordReport.java:161)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Here is the data file:
A100 99.99 10 Canon PowerShot-135
A200 149.99 50 Panasonic-Lumix T55
A300 349.99 20 Nikon- D3200 DSRL
A400 280.99 30 Sony- DSC-W800
A500 97.99 20 Samsung- WB35F

Here is the data file for the modified field values.
B100 98.00 10 ABC1010
B200 97.00 15 DEF1020
B300 96.00 10 GHI1030
B400 95.00 05 JKL1040
B500 94.00 01 MNO1050

View Replies


ADVERTISEMENT

Copying Data Objects To Serializable Objects

Jul 27, 2014

I am currently working on a project where I need to return data from a database over RMI to a client who requests it. Some of the fields in the Data Object can not be seen by the client so I need to create another object to send over the network instead. The method I use is this...

public static SerializableObject createSerializableObjectFromDataObject(DataObject dataObject){
SerializableObject serializableObject = new SerializableObject();
serializableObject.setField(dataObject.getField());
serializableObject.setAnotherField(dataObject.getAnotherField());
return serializableObject;
}

Is there a better way of doing this? I am creating many subclasses DataObject which all require this static method to be implemented and I can't push it into the superclass because each one needs custom behaviour.

View Replies View Related

Servlets :: Why Objects Stored In Session Should Be Serializable

Feb 5, 2015

Why is it that objects stored in session object should be serializable?

View Replies View Related

Swing/AWT/SWT :: Sending Serializable Object Between Two JFrames

May 2, 2014

I have two JFrames the first one is a log in screen where i log in to the log in server that i made using RMI . If the log in succeeds i send the user object witch is Serializable to the second JFrame.

The user object is received from the server. This object contains all the information needed in the second JFrame. This second JFrame is made inside the first one and i defined a method with witch i send this user object to the second frame.

Now the problem arises , while inside this method i am able to read all the data i need from the user object. At the end of the method i save the reference of this object in a new User object defined in the second JFrame ,because i need it later on.

But soon as this method finishes , for some reason my reference to the object becomes null , and i cant use it later on.

View Replies View Related

Modify And Use A GUI To Set Variables Setter Methods

Feb 24, 2012

i'm trying to modify and use a GUI to set the variables the setter methods. while the code seems valid to me and should work perfectly, i get ArrayIndexOutOfBounds and StringIndexOutOfBounds and respectively lines 111 and 134. i'm am not the original author of this code, all i want is to get it to work fine.

1package de.kugihan.dictionaryformids.dictdconv;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.OutputStreamWriter;
6

[code]....

View Replies View Related

Does JavaFX Methods And Objects Works Into Java Application

Nov 22, 2014

I was doing a project in a usual Java Application, but now maybe I have to use some tools that are contained in javafx.scene.media.MediaPlayer so I wonder if can it all work? Will javafx methods and such works?

I have to use javafx.scene.media.MediaPlayer beacuse I have to create a very simple audio player (one jbutton and one jcombobox).

View Replies View Related

Exception Handling - Block Cannot Modify Primitive Values

Sep 23, 2014

class MultipleReturn {
int getInt() {
int returnVal = 10;
try {
String[] students = {"Harry", "Paul"};
//System.out.println(students[5]); //if i remove comment

[Code] .....

View Replies View Related

Sorting Objects By A Number Of Fields

Mar 17, 2014

I am trying to find a concise way to write the sort methods for my class. I am supposed to make a program that can sort objects by a number of fields: year, rank, artist and title.

I used an idea from this thread : java - Sorting a collection of objects - Stack Overflow

And I am trying to use the custom comparator for my sort methods. However for some reason, the sortingBy variable fails to recognize any of the enum types.

Whenever I try to set the sortingBy variable equal to one of them, for example:

Java Code:

private Order sortingBy = Year; mh_sh_highlight_all('java');

I get a "Year cannot be resolved to a variable" error.

What I want to be able to do is make it so every time a specific method is called, say, for example sortTitle(), sortingBy will change to Title, then the SongComparator will sort using the case Title.

Is it possible to do this? I can't figure out how to modify SongComparator's object variables that way.

Java Code:

import java.util.Comparator;
public class SongComparator implements Comparator<Song> {
public enum Order {Year, Rank, Artist, Title}
public Order sortingBy;

[Code] .....

View Replies View Related

How To Modify Property Of Object Stored In List

Jul 31, 2014

I'm facing a problem in below scenario:

List lst = tDAO.executeReport();

lst contains list of objects and one of the objects contains the property bDate(Timestamp) which has the value 28-2-1989 00:00:00.0, now I just wants to change the value into 28-2-1989 and store it back into the List as a Timestamp. how can I do that.

View Replies View Related

How To Grab Graphical Object From Arraylist And Modify It

Oct 26, 2014

I am trying to grab a graphical object from an arraylist, and reposition its coordinates on a Jframe when adding it. My program of course deals with strings, and once it sees specific words in my console, some method is called that adds, removes, or otherwises modifies certain objects on screen.

Here I want to say something like move(object[1],xpos,ypos) which will move a certain object from a specified point in the array, and move it to new x and y positions on the JFrame. I use a different class that extends a graphics program, so when I say add(something,x,y) it draws the object onscreen where I want it. These are some relevant, though incomplete, methods that should move an object already painted on screen:

Console class

Java Code:

public void doMoveCommand(String cmd, String arg, String xpos, String ypos) {
int x = Integer.parseInt(xpos);
int y = Integer.parseInt(ypos);
if (cmd.equals("posMake") && arg.equals("star")) {
box.moveStar(box.historyG.get(1), x, y); //historyG is an arraylist of GPolygons
freeCommand();

[Code] ....

When I say makepos(whatever) I am getting a arraylist out of bounds exception. How I might be able to accomplish moving objects already on screen?

View Replies View Related

Fields / Getter And Setter Methods And Constructors

Mar 12, 2015

A blood clinic has hired a team of software developers to build a custom application to track patients. The clinic needs to keep a record of each patient and his or her blood data. Ultimately, they want all of the information stored in a database. As a starting point, the development team leader informs the team that the application has to have a set of core classes that represent the “real-world” entities the program needs to deal with. As a developer on the team, your job is to build a Patient class along with a BloodData class so that each Patient contains a BloodData object. This principle is known as “composition.”

Building the Framework Begin by creating a public Java class named PatientBuilder that contains a main method. Then, in the same file, create a non-public class named Patient and another named BloodType. Save the file as PatientBuilder.java. Note: If this was a real development project, you would put each class into it’s own file and put the files in the same folder. By combining them all into one file, we avoid having to submit three separate files, making it easier to keep all your work in one place.The BloodData Class This class should hold all of the attributes to hold the data the clinic needs for a given patient’s blood. Implement the following capabilities to accomplish this objective:

• Create a field to hold a blood type. The four possible blood types are O, A,B, and AB. For this project, you can simply define the field as a String.
• Create a field to hold the Rh factor. The two possible Rh factors are + and –.For this project, you can simply define the field as a String.
• Create getter and setter methods for both fields.
• Create a default constructor that sets the fields to “O” and “+”
• Create an overloaded constructor that accepts two parameters – one for a proposed blood type, and another for a proposed Rh. The constructor
should call the set methods and pass these parameter variables in to them.The Patient Class This class should hold all of the attributes to hold the data the clinic needs for a given patient’s blood. Implement the following capabilities to accomplish this objective:
• Create a field to hold an ID number along with get and set methods.
• Create a field to hold the patient’s age along with get and set methods.
• Create a field to hold the BloodData for a Patient. When declaring the field, initialize it by instantiating a BloodData object. Create get and set methods.
• Create a default constructor that sets the ID to “0”, age to 0, blood type of the BloodData object to “O”, and Rh value of the BloodData object to “+”.
• Create an overloaded constructor that accepts the following parameters: ID,age, blood type, and Rh. The constructor should call the set methods for the field associated with each parameter.The PatientBuilder Class.This class should contain the main method from which the program runs. In that method, implement the following functionality:• Ask the user for the ID, age, blood type, and Rh factor of a Patient.
• Create a Patient object, passing all of the data obtained from the user into the object.
• Write the code necessary to display the ID, age, blood type, and Rh factor of the Patient by calling the appropriate get methods of the object.

MY CODE ( which does not compile since it is wrong...)

import java.util.Scanner;
public class PatientBuilder
{
public static void main(String[] args){
String patientID;
int patientAge;
String patientRh;
String patientBlood;

[code].....

View Replies View Related

Modify Internal Object Structure Using Any Design Pattern

Mar 31, 2014

I have a design scenario here which is quite interesting and complex. I have a Java class structure as follows,

class A
{
     class B;
     innerClass B
     {
          List<class C> listofC;
          innerClass C
          {
               String attribute1;
               String attribute2; // Their getter setters
          }
     }
}               

So I have this as an API. Now my challenge is that I need to add one more property to inner class C.  i.e attribute3 in innerClass C. I need to do this without disturbing the code in class A by extending these classes or writing a new wrapper, so I can use class C with new properties .

I hope this should be achievable through any design pattern either at runtime or design time.

View Replies View Related

Sending Values For GUI Fields

Jul 9, 2014

I have a simple class, called InputManager, which has methods that communicate with another class DatabaseManager which has access to Database which is actually object that contains information about actual database.

My JFrame has Navigation class (extends JPanel). There I put all needed JButtons, JLists and so on.. This class has instance of InputManager and when user interacts with GUI components inside of it, Navigation calls InputManager methods, it then analyses the call and calls needed methods on DeviceManager, which then sends queries to Database and etc.

The problem is, that at the beginning (when user runs a program), I want to get information (which is actually values for my GUI components ) from Database. It could, for example, create another DatabaseManager and connect to the Database to get information, but it is okay to have two objects?

I was also thinking about sending same DatabaseManager object through InputManager to Navigation, but it seems strange for me to have two objects, which can interact with each other in both directions(Class A has instance of Class B, and Class B has instance of Class A). Is there anything bad this can do?

Also what I noticed that it seems odd that GUI class would have to get information from database. (Logic in GUI class).

View Replies View Related

Modify Common Object References In Lists - MyArrList And UrArrList

Jan 29, 2014

"What happens if you modify the common object references in these lists, myArrList and urArrList ? We have 2 cases here: In the first one, you reassign the object reference using either of the lists. In this case, the value in the second list will remain unchanged.In the second case, you modify the internals of any of the common list elements - in this case, the change will be reflected in both lists."

I have written the following code, which tests the first case mentioned above, and i get the output as expected: myarrList remains unchanged. How can i test the second case ? My thoughts are ....'second case is untestable the following code, because String is immutable. I need to use StringBuilder or something else to write code for test of second case mentioned'.

ArrayList<String> myarrList = new ArrayList<>();
myarrList.add("one");
myarrList.add("two");
ArrayList<String> urarrList = new ArrayList<>();
urarrList.add("three");
urarrList.add("four");
System.out.println("ArrayLists setup");

[code]....

View Replies View Related

Counts Ticket Objects In ArrayList - Accessing Private Fields

Nov 4, 2014

I am trying to create a method for my "ticketmachine" object that counts ticket objects in an arraylist of "ticket" objects with a specified field "route". The method works if i change the field type of the ticket class to public, however according to the homework task "route" of the ticket class needs to remain a private field.

Here is my code for the method.

public int countTickets(String route) //HAD TO CHANGE Ticket class FIELD "ROUTE" TO PUBLIC!!!!
{
int index = 0; //returns the number of Ticket objects in the machine with the specified "route".
int ticketCounter = 0;
while (index < tickets.size())

[Code] ....

Is my general approach to the problem wrong? also is there a way to access a private field?

View Replies View Related

Get Values From Database And Set Them In Text Fields?

Apr 24, 2014

I have a button in which I need to do the following:

After I click it It reads the values given in a jtextfield1 and in the other jtextfields I need it to settext in them from the database I mean if I have for example id=1 when I click the button it goes to the database and find the id 1 then write the infos in the other textfields

View Replies View Related

Enum Properties / Values / Fields

Jul 11, 2014

So I have an Enum file with 119 constants and each constant of that type has 20 fields that come with it. All the fields are the same type and named the same (e.g. there are 119 of Object obj, one for each constant), and I want to run the same methods over them. Since the Objects of the same type are named the same for each constant, I just have them named explicitly in get-er methods.

This worked fine when I just put all 20 fields through the constructor and set them as fields under all the constants. But I realized that if I wanted to make an instance of this Enum class, I'd have to enter in all 20 fields when they are all a set of Objects with unique values. So I then put them as fields under their own respective constant to make it easier to create instances of this enum. But now my methods don't work.

A) I don't really understand why they don't work anymore?
B) Is there a way to fix it without putting all the methods under each constant?

Example:

public enum MyEnum {
AAA {
private MyObject obj = new MyObject (3.0);
},
BBB {
private MyObject obj = new MyObject (1.5);
},
CCC {
private MyObject obj = new MyObject (6.5);
},
DDD {
private MyObject obj = new MyObject (3.5);
};

public double getObjVal() {
return obj.value(); // it can't find this obj should I move it up to where the constants are declared?
}
}

View Replies View Related

JSF :: Viewstate Stored In Session And Values Of Input Fields

Sep 11, 2014

Many times I have read that JSF stores de view state in session and one of the things that are stored are the values of the form fields, right? But these input field values are not stored in session, I mean, they depend of the scope of the managed bean attached to the view. If I have a view and its attached to a view scoped bean with properties for the form fields, the values will dissapear if I go to other view because the bean is view scoped so I cant say the values of the form are stored in session, right?

View Replies View Related

Modify Class Time2 To Include Tick Method That Increments Time Stored In Object By One Second

Jul 9, 2014

Modify class Time2 to include a tick method that increments the time stored in a Time2 object by one second. Provide method incrementMinute to increment the minute and method incrementHour to increment the hour. The Time2 object should always remain

a) incrementing into the next minute,

b) incrementing into the next hour and

c) incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).

how to manage case 4 stuff and what's the problem of this CODE.

import java.util.Scanner;
public class Time2Test
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
Time2 time = new Time2();
// input
System.out.println( "Enter the time" );
System.out.print( "Hours: " );
time.setHour( input.nextInt() );

[code]....

View Replies View Related

Creating Objects From Methods?

Mar 20, 2014

public class demo
{
Public class static void main(String[]args) {
//Creating a variable that will be a reference to the object
Peoples person_one;

[Code] ....

I have assembled this code below that has a void method which will creat a new object. Problem I encounter is that in

Create_object(person_one);

the person_one has an error saying not initialized. I'm jus trying to learn on my own ways here and practice so may know what's wrong with this? I know I can use a return object from methods but what about this approach?

View Replies View Related

Creating New Objects Within Methods?

Sep 14, 2014

I want to make a program where users are prompted to enter a username and a password and have these two values create a new instance of the Object User. But I'm not sure where to start.

import java.util.Scanner; 
public class Main {
public static void main(String[] args) {
createUser();

[Code] ....

how to take username + password and put it into an object.

View Replies View Related

Calling Methods And Instantiating Objects

Dec 14, 2014

I have two classes, MonsterGame (shown below) and Monster. Monster has a public accessor method to get it's private character variable, which represents the first character of the name of the Monster in question.

I'm just a bit confused as to why am I unable to cycle through the array of Monsters I have in the MonsterGame class and call

m.getCharacter(); (pretty much the last line of code)
public class MonsterGame{

private static final char EMPTY_SQUARE = ' ';
private char[][] gameBoard = new char[10][10];
private Monster[] monsters = new Monster[4];
private int maxXBoardSize = gameBoard.length -1;
private int maxYBoardSize = gameBoard[0].length -1;

[Code] ....

I understand that there need to be instances of objects to call methods, but is that not the case here? the Monster objects are have already been created, no? Do I need to create an index for the array? is the for loop not enough?

View Replies View Related

JSP :: How To Differ Between Fields That Not Exist To Fields That Are Null

Dec 7, 2014

how to differ between fields that are not exists to fields that are null? because in my api when someone wants to delete a field he sends null instead of a value. and if he doesnt want to effect this feild he doesnt send it.

{
"a" : {"1","2"},
"b" : "hello"
}
{
"a" : null,
"b" : "hello"
}
{
"b" : "hello"
}

View Replies View Related

Involving Array Of Objects And Referencing Methods Of Each

Nov 26, 2014

I'm working on a project that involves the following:

-Creating a superclass of bankaccounts
-Creating two subclasses, checkingaccount and savingaccount
-Each of the two subclasses has different methods (writeCheck for checking, for example)
-Both types are created in a main class bank and stored in the same array

So let's say a user goes through the menus and creates a few savingAccounts and a few checkingAccounts (stored in the accounts[] array). Then, to write a check from one account, the user can enter the account number (a string), and the method will use a for loop to cycle through the array until it hits an account number match. Then it checks that it's the correct account type and calls methods from the subclass.

Problem here is that some methods work and some don't. In the following example:

for (BankAccount account: accounts) {
if (account.getAccountType().equals("Checking")) {
do {
if (account.getAccountNumber().equals(accountNumber)) {
amount = Double.parseDouble(JOptionPane.showInputDialog(

[Code] .....

The getAccountNumber method works but writeCheck is throwing an error. I tried creating a method in the superclass and overriding it in the subclasses but with no success.

View Replies View Related

How To Modify Site Exception List In Java Control Panel

Apr 2, 2014

When I am watching scjp mock test, I am getting error about security.

I am trying to add www.javaranch.com to site exception list in java control panel. I can see security tab in java control panel, there I find field for Site Exception List. When I am add [URL] .... to this site exception list, list is not getting populated.

Why is that ? Why am I unable to add this site to list ?

I am using 64-bit system and have downloaded and installed 64 bit update patch 51.

View Replies View Related

Passing Values And Objects

Oct 28, 2014

Whether I pass primitives or objects the original value does not change. Is this expected result?

Java Code:

public class Class {
public static void main(String args[]) {
int x=3;
doItPrim(x);
System.out.println(x);//3
Integer i=new Integer(3);

[Code] ......

View Replies View Related







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