Getting Class Cast Exception While Using Object Input Stream

Jun 5, 2014

I'm developing an application to track the status of a production flow-line and have hit a bit of a snag. When attempting to read saved data I run into this:

Exception in thread "main" java.lang.ClassCastException: flowline.End_Of_File cannot be cast to flowline.Operation
at flowline.Station.checkLoadPreviousStationStatus(Station.java:91)
at flowline.Station.main(Station.java:212)
Java Result: 1

I've been reading up on different methods to saving and retrieving data and have decided ObjectInputStream would be the best option.

The save method works fine, I opted to use a EndOfFile class to determine when I've reached the end of the input stream. The problem is, when my loop encounters this object, it doesn't terminate the loop.

public void checkLoadPreviousStationStatus() throws FileNotFoundException, IOException,
ClassNotFoundException, EOFException, TempArrayOutOfBoundsException{
Object loadOpn = null;
End_Of_File eof = new End_Of_File();
File f = new File(fileName);

[Code] .....

The Operation cast is a cast to the objects my LinkedList contains. The highlighted line is where the exception occurs.

View Replies


ADVERTISEMENT

Unable To Create Audio Stream From Input Stream

Jan 18, 2015

package timerApp;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import sun.audio.*;
import java.io.*; 
public class timerDriver
{
static int interval;

[Code]...

So I'm trying to make a program that plays an mp3 file after a timer reaches 0, but i keep receiving the error "could not create audio stream from input stream" the audio file is 3.44 MB and 00:03:45 minutes long if that's a problem

View Replies View Related

Trying To Cast One Object As Another

Oct 13, 2014

Im trying to loop through a hashmap of objects. They are defined as People objects. People has two subclasses , Instructor and Student. As I am looping through the map of People, I am searching for class Instructor. If I find it, I want to access its method getDepartment in a println by casting to Instructor. When I do I get a runtime error:

Exception in thread "main" java.lang.ClassCastException: java.util.HashMap$Node cannot be cast to uStaff.Instructor
at uStaff.PersonApp.menu(PersonApp.java:108)
at uStaff.PersonApp.main(PersonApp.java:21)

//Instantiate the different Person, student and instructor objects
Person thisPerson = new Person(01,fName,mName,lName,email,ssn,age);
Student thisStudent = new Student(02,"Stacey","Marie","Morgan","smorgan@gmail.com","213-45-6789",20);
thisStudent.setMajor("music");
Instructor thisInstructor = new Instructor(03,"Joe","Douglass","Wells","joe@drumhaven.com","555-98-3029",46);
thisInstructor.setDepartment("Computer Science");

[code]....

View Replies View Related

Cannot Cast From Object To Double

Nov 9, 2014

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("js");
String expres = calc.getText().toString();
calc.setText(expres);
try {
calc.setText(((double)engine.eval(expres)) + "");
} catch (Exception e) {
}

This line: calc.setText(((double)engine.eval(expres)) + "");

*The error message for this line: Cannot cast from object to Double

View Replies View Related

How To Cast PrintStream Object In ObjectOutPutStream

Jul 10, 2014

I want to write formatted output on a notepad file using ObjectOutputStream but I am not getting it in human readable formatted form

Here is my person class

public class Person implements Serializable {
private String firstName;
private String lastName;
private int age;
public String getFirstName() {
return firstName;

[code]....

I want to know how to use printStream.print() like method to write formatted output.

View Replies View Related

Input From Different Classes - Cannot Cast Int To String

Oct 18, 2014

I am working on a program where I want the user to input multiple classes.

One int, and one String.

Can this be done? if so, how?

I have a tried to get input from both, like in the code below:

Java Code:

import java.util.Scanner;
public class ForumFlowchart {
public static void main(String[]args){
//Creating scanner.
Scanner input = new Scanner(System.in);
//Get information about job
System.out.println("Type in Int");
int i1 = input.nextInt();

[Code] ....

If I keep my input in the same class, I get the error "Can not cast int to string". My question is, it is possible to get an input from both an Int and a String in the same program?

View Replies View Related

Possible To Declare Two Data Input Stream?

Aug 3, 2014

I mean i need to access to two txt file at the same time so i did this in method:

DataInputStream din= new DataInputStream(FileInputStream("vip.txt"));

DataInputStream din2= new DataInputStream(FileInputStream("corporate.txt"));

But there are errors when i compile and they pointing to these two lines.

View Replies View Related

Type Safety - Unchecked Cast From Object To LinkedList (EventData)

Feb 28, 2014

I am getting "Type safety: Unchecked cast from Object to LinkedList<EventData>" in eclipse for a piece of code stated below

public LinkedList<EventData> loadFromFile(File file) {
queue=new LinkedList<EventData>();
//Some piece of code
return (LinkedList<EventData>)queue.clone(); //--->getting warning here
}

I know that because clone() method is returning Object, hence compiler doesn't have type information that's why showing warning. I don't want to suppress this warning instead i want to fix it.

View Replies View Related

First Character Is Missing While Reading Input Stream

Jul 10, 2014

This is my code

// TODO Auto-generated method stub
File file=new File("D:/input.txt");
java.io.FileInputStream is=new java.io.FileInputStream(file);
int a=is.read();
try {
while((a=is.read())!=-1) {
System.out.print((char)a);

[Code] .....

My text in file is :my birthday Hello World

But in out i see only:y birthday Hello World

Why is first character is missing here?

View Replies View Related

Second Packet Read From Input Stream Has Incomplete Data

May 8, 2015

I a simple server that receives bytes using TCP and then saves them to a file stream.Through many tests I have seen that the first packet received is always just the filename with no other data. The second packet received only has one byte and it is the first letter of the input text file. After this all packets are sent correctly, but I can't seem to figure out what is messing up the second packet. It also appears that the last packet is written twice.

Here is an example Input/Output: [URL] .....

InputStream in = clntSock.getInputStream(); //server's input stream - gets data from the client
OutputStream out = clntSock.getOutputStream(); //server's output stream - server sends data to the client
byte[] byteBuffer = new byte[BUFSIZE];
int count = in.read(byteBuffer, 0, BUFSIZE);
String firstRead = new String(byteBuffer, 0, count);

[Code] ....

Another peculiarity to me is that the second to last packet is always just "-" and then the last packet has the remainder of the magic string which terminates the file output stream.

I am aware that it is not safe to make the assumption that the file name will be sent in one go before the loop, but I wil fix that later. I am not concerned about it at all now. THe problem si that the loop should account for any amount read in, but for some reason the first packet always contains one letter, and the second packet picks up around 16000 bytes later for some reason. Why would this be?I can't edit my above post so here is the code with code tags.

InputStream in = clntSock.getInputStream(); //server's input stream - gets data from the client
OutputStream out = clntSock.getOutputStream(); //server's output stream - server sends data to the client
byte[] byteBuffer = new byte[BUFSIZE];
int count = in.read(byteBuffer, 0, BUFSIZE);
String firstRead = new String(byteBuffer, 0, count);

[Code] ....

View Replies View Related

Servlets :: JPEG Image Upload - Data Input Stream

Jan 25, 2014

Iam trying to upload jpeg image.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

[Code] ....

In servlet the below code

DataInputStream din = new DataInputStream(request.getInputStream());
byte[] data = new byte[0];
byte[] buffer = new byte[50];
int bytesRead;
while ((bytesRead = din.read(buffer)) > 0 ) {

byte[] nData = new byte[data.length + bytesRead];
System.arraycopy(data, 0, nData, 0, data.length);
System.arraycopy(buffer, 0, nData, data.length, bytesRead);
data = newData;
}

What i see is servletinput stream does't have any values.It does't even enter while loop.

View Replies View Related

How Is Class Cast In Method Parameter

Jan 15, 2014

how is cast a class at a methods parameters? i have a problems in a methods paramter. i draw red line my exception and mymethods is orage color ....

Caffe drink = new Caffe();
CoffeCup cup = new CoffeCup();
cup.setTempeture(100);
drink.drinkcaffe((CaffeCup)(cup.getTempeture()));

[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

Throwing Exception With Exception Class?

Sep 29, 2014

Right, so I got this program. It takes input from the user and assigns it to fields on an object. But, it's meant to check the users input. If the user enters bad input, it's supposed to throw this exception. For each of these exceptions, theres a class specifically for it.

public class PayrollDemo
{
public static void main(String[] args)
{
Payroll pr = new Payroll ("Test Name", 234);
System.out.println("Current Employee Information.
");
System.out.println("Name: " + pr.getName());
System.out.println("ID: " + pr.getID());
System.out.println("Hourly Pay Rate: " + pr.getHourlyPayRate());

[Code] ....

And this is the exception class.

public class InvalidNameException extends Exception
{
/**
No-arg constructor
*/
public InvalidNameException()
{
super("Invalid name");
}
}

PayrollDemo.java:43: error: cannot find symbol
InvalidNameException.InvalidNameException();
^
symbol: method InvalidNameException()
location: class InvalidNameException
1 error

It's just meant to tell the user that they entered an invalid value, which would mean if they entered an empty string instead of a name.

View Replies View Related

Stale Object State Exception

Aug 17, 2014

Caused by: javax.persistence.NonUniqueResultException: result returns more than one elements
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryI mpl.java:109)

What are the common reasons for the above exception and the suitable fixes?

View Replies View Related

Exception In Thread Main - Cannot Format Given Object

Apr 16, 2014

I am working on a program that will allow a user to input grades for a class of four students who have taken two tests. Based on the grades entered, the program will calculate the averages of the two tests for each student and display it along with their respected letter grades.

Now I can get the program to compile successfully, but after inputting the grades in, I get the error message saying that it cannot format given object as a number. I am using 4 arrays to execute this program and maybe that's why I'm having the trouble? I'm not sure because I am still fairly new at this stuff and can't sen to resolve it.

I was having problems for a while and then finally got excited when I got it to compile without any errors and now I'm getting an error inside my program. All I need to do is format the numbers of the grades into something like: 000, and each of the averages as 000.0. I understand how to do it because I have done it in another program I've done in the past. I just don't know how to fix this error that is coming up.

here is my code:

import java.util.Scanner;
import java.text.DecimalFormat;
public class TestAverage
{
/**
* A program that will store and process 2 test scores for a class of 4 students.
* The program will prompt for the test scores as shown above in the sample run.
* After all the data is entered,the program will display the score for test 1, test 2 .
* The average of the 2 tests and the letter grade for the class for each student in a tabular format.
*
*

[code]....

View Replies View Related

Declaring Object Using Interface Implemented By Its Class Vs Declaring Object Using Class

Apr 8, 2014

Suppose you have a generic Dog class of the pet type that implements a DogInterface of the pet type. What is the difference between;

DogInterface<pet> Rex = new Dog<pet>();

and

Dog<pet> Tye = new Dog<pet>();

In what situations might you want to use Rex instead of Tye?

View Replies View Related

Converting String To Date Object - Runtime Exception

Jan 26, 2014

I am getting Run time Exception while converting String object to Date Object ....

java.text.SimpleDateFormat@b9b195a0
java.text.ParseException: Unparseable date: "14-07-2012"
public static void main(String[] args) {
String time = "14-07-2012";

[Code] .....

View Replies View Related

What Is A Class Object Not Class Named Object

Mar 24, 2014

"You might wonder what happens when a static synchronized method is invoked, since a static method is associated with a class, not an object. In this case, the thread acquires the intrinsic lock for the Class object associated with the class. "What is a Class object associated with a class. Google search rather finds material about the Object class.

View Replies View Related

Program Won't Compile / Input Mismatch Exception

Feb 11, 2015

I am trying to read contents from a file and display them to the user. However, when I enter the file into the program I get the following error: "exception in thread 'main' java.util.MismatchException. What am i doing wrong?

import java.util.Scanner;
import java.io.*;
public class Project1{
 public static void main(String[] args) throws FileNotFoundException {
double balance;
double item1Price;

[code]....

View Replies View Related

Expression Calculator - Input Mismatch Exception

Nov 2, 2014

import java.util.*;
public class CalculatorProjectVincent {
public static void main(String[] args) {
System.out.println("Welcome to this incredibly uneeded and redundant calculator program!");

breaker();
Scanner kb = new Scanner(System.in);
System.out.print("Enter an expression or EXIT to end the program. > ");

[Code] ....

When I run the program and input "v 100", it gives me the following:

----jGRASP exec: java CalculatorProjectVincent

Welcome to this incredibly uneeded and redundant calculator program!
- - - - - - - - - -
Enter an expression or EXIT to end the program. > v 100
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)

[Code] ............

View Replies View Related

How To Change State Of Object - Main Method Exception Error

Jan 8, 2015

The error I get when I execute my java file.

Exception in thread "main" java.lang.NullPointerExceptionat DogTestDrive.main(DogTestDrive.java:19)

Here is the source

class Dog {
int size;
String name;
void bark () {
if (size < 60) {
System.out.println("Woof woof");

[code]....

Some background: I'm reading "Head first Java 2nd edition" and I'm going through the examples which is showing me how to change the state of an object. The original code looks like the code below, however the previous chapter went over creating array's of an object, so I created an array of the object "Dog" and wanted to re-write it this way. To my understanding, it should work but it's giving me that error when I execute it. The error itself isn't very clear, if I could get a line number pointed to, that would work.

class Dog {
int size;
String name;
void bark() {
if (size > 60) {
System.out.println(“Wooof! Wooof!”);
} else if (size > 14) {
System.out.println(“Ruff! Ruff!”);
} else {
System.out.println(“Yip! Yip!”);

[code]....

View Replies View Related

How To Use File Input / Output In Addition To Exception Handling

Dec 2, 2014

I am trying to learn how to use file input/output in addition to exception handling... The problem is my textbook wrote this chapter for a version of Java that hasn't come out yet, so everything I do "according to the textbook" doesn't work. any feedback on correcting these exception errors because I am not sure what is causing them or how to fix them.

I was able to have it display the name of the book in the Book.txt file, but when I added the second part if the file doesn't exist, that's when the errors came up and it wouldn't compile.

import java.io.*;
import java.util.*;
public class DisplayBook
{
public static void main(String[] args) {
try {
File book = new File("Book.txt");
FileInputStream in = new FileInputStream(book);

[Code]...

These are the compilation error messages I am receiving: (I have managed to get it down from 7 errors to just 4, but now I'm stuck)

DisplayBook.java:15: error: unreported exception IOException; must be caught or declared to be thrown
while ((letter = in.read()) != -1) //if file exists, displays book title
^
DisplayBook.java:24: error: unreported exception FileNotFoundException; must be caught or declared to be thrown

[Code] ....

4 errors

View Replies View Related

Accessing Parent Class Method Using Child Class Object?

Feb 4, 2015

I want to know is there any way we can call parent class method using child class object without using super keyword in class B in the following program like we can do in c++ by using scoop resolution operator

class A{
public void hello(){
System.out.println("hello");
}
}
class B extends A{
public void hello(){
//super.hello();
System.out.println("hello1");

[code]....

View Replies View Related

Pass Private Final Class Object To Another Class Constructor

Aug 28, 2014

can we pass private final class object to another class constructor?

View Replies View Related

How To Create Object For Multiple Class Inside Single Class

Apr 22, 2015

How to create object for "class B" and call the "function_B" from other different class D where class D has no connection with class A? Here is my program.

public class A(){
void print(){}
}
class B{
void function_B(){}
}
class C{
void function_C(){}
}

Here, A, B, C are in the same package. But class D is in different package.

View Replies View Related







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