import java.lang.*;
class InvalidValueException extends IllegalArgumentException {}
class InvalidKeyException extends IllegalArgumentException {}
class BaseClass {
void foo() throws IllegalArgumentException {
throw new IllegalArgumentException();
[Code] .....
Which one of the following options correctly describes the behavior of this program? And the answer is (definitely) --> The program will print : InvalidKeyException exception, but when i saw the explanation, it tells
It is not necessary to provide an Exception thrown by a method when the method is overriding a method defined with an exception (using the throws clause).
I don't know, but i think it will compiled because the Exception that is thrown by the foo method in DeriDeri class is inherited from unchecked exception.. so it is not necessary to declare throws statement on its method.. and if the exception was checked exception the answer must be different right?
value1 = text1.getText(); value2 = text2.getText(); Connect c = new Connect(value1,value2); if(c.check()== true) { Menu start = new Menu(c.getID()); dispose();
[code].....
I get an error "unreported exception java.SQLException; must be caught or declared to be thrown" how can i solve this I have taken a few basic classes but never got to exceptions.
There are times that my methods need to report the caller about many kinds of errors, represented as checked exceptions. This makes my methods look like very convoluted. It happens mostly when I work with stateless EJBs and databases. Sometimes I end throwing even 8 different exceptions! Are they too many?
Many of them inherit from common exceptions, so I've been tempted to throw the parent classes. But I've quickly discarded that option because I've read that it's a bad practice, since a method may throw only a subset of the children of that parent class, not all.
Finally, I've studied these possibilities:
1. Throwing the parent class (mentioned above). 2. Throwing a common exception with an error ID or code as message. 3. Throwing a common exception with an enum as member, as if it were an ID or code (safer than the #2).
All them show the same defect that the #1. However it's more a conceptual problem than a technical one, because my callers always use the same mechanism to treat every "specialization" of the same exception, without worrying about if the method really can return it or not.
Java Code: class A { int x=5; } class B extends A { int x=6; } public class CovariantTest { public A getObject() {
[Code] ....
And this is the output I get:
sub 5
I am unable to figure out how this is outputting 5 instead of 6. The getObject method of SubCovariantTest is obviously the one being called, and it returns a new B(). So why am I getting class A's x value? I thought since I was getting a B object returned that I would get B's x value.
TextButton up = new TextButton("up", textButtonStyle);
and .addListener is just one of the methods "TextButton" has (actually I think its inherited from "Button" but that doesn't matter).
Basically my question is what's going on inside the parentheses? From what I see its a new instance of "ClickListener" but then suddenly they override an actual method within. Is this simply just a way to override a method from the ClickListener class or is it something else?
If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you're calling the supertype version of the method.
is this true? maybe i'm misunderstanding it, but i thought the JVM looks at the object at run time and checks the object type. the context of the quote is about checked exceptions, but it seems like the statement should stand regardless of context. but this doesn't back up my experience. for example:
public class Test{ public void print(){ System.out.println("Super"); } public static void main(String[] args){ Test t = new SubTest();
[Code] ....
Will invoke the subclass method. like i said, maybe i'm missing something.
I don't understand, why when in the constructor of the superclass A the method init() is called (line 4), the overridden version of the subclass is invoked (line 26) and not the superclass version (line 7):
class A { public A () { init(); } public void init() { System.out.println("test");
[Code] ....
I would have guessed that above code prints
test 1
But instead you get a NPE (because when the constructor of B is invoked
public static void main(String[] args) { new B(); }
Then there is first the implicit call to super:
public B() { s = " "; init(); }
Which is the constructor of A:
public A () { init(); }
But here now this init() method is not the version of A ( public void init() { System.out.println("test"); }) but the overriden version of the subclass (B): public void init() { System.out.println(s+=s.length()); }...
Which throws an NPE of course, because the initialization of s has not occured yet (it happens only after the implicit call to super() has finished (see public B() { s = " "; init(); }))
I am just learning how to throw exceptions, and I'm stuck on the last part,
Here is where I created the exception that receives a string consisting of ID and wage.
public class EmployeeException extends Exception { public EmployeeException(String Employee) { super(Employee); } }
Here is where I created the Employee class with the two fields. I also believe I am throwing the exception if the hourly wage is < $6 or > $50.
public class Employee { int idNum; double hourlyWage; public void Employee(int empID, double empWage) throws EmployeeException { idNum = empID; hourlyWage = empWage;
[Code]...
Now, I need to write an application with three employees and display a message when an employee is successfully created or not. This is what I have so far... I'm trying to get it to work with one employee, and then I can easily go back and add two more.
import javax.swing.*; public class ThrowEmployee { public static void main (String[] args) { try { Employee one = new Employee(542, 15.20); }
[Code
The current compile error that I'm receiving is: ThrowEmployee.java:12: error: constructor Employee in class Employee cannot be applied to given types;
Employee one = new Employee(542, 15.20); ^ required: no arguments found: int,double reason: actual and formal argument lists differ in length 1 error
I have public void Employee(int empID, double empWage) in my Employee class, so why is it saying that no arguments are required? Not sure if I'm missing a step with throwing exceptions, because this error has always been a simple fix when I've come across it in the past?!?
public class ThrowException { public static void main (String[] args) { var x=prompt("Enter a number between 0 and 10:",""); try { if (x>10){ throw "Err1"; } else if (x<0){ throw "Err2"; } else if (isNaN(x)){ throw "Err3"; } } catch(er){
[code]...
It's telling me where catch(er) is: <identifier> expected..I've watched videos, but no one seems to encounter this error....am I missing a segment of code?
I have a file greenGrow.txt, and every three lines of the file has a last name, first name, and yard size. Every time a Customer object is created, I need to read the file 3 lines and assign the object a last name, first name, and yard size.
Snippet of my code:
import java.io.*; import java.util.*; public class Customer { private String lastName; private String firstName; private int yardSize;
[Code] .....
My issue is that I cannot call readFile() from the constructor, and I'm assuming that's because I throw Exception on readFile(). Is this a simple fix, or am I looking at it the wrong way?
imagine I build this this device that takes in impulses... 0 and 1..but I need an interface with buttons to decide when to throw a 0 or a 1...would java be a good language to control this?I'm having difficulty to make this path... interface to circuit...
Dice are used in many games. One die can be thrown to randomly show a value from 1 through 6.
Design a Die class that can hold an integer data field for a value (from1 to 6).
Include a constructor that randomly assigns a value to a die object. Appendix D contains information on generating randomnumbers. To fully understand the process, you must learn more about Java classes and methods. However, for now, you can copy the following statement to generate a random number between 1 and 6 and assign it to a variable. Using this statement assumes you have assigned appropriate values to the static constants.
Also include a method in the class to return a die's value. Save the class as
Die.java.
Write an application that randomly "throws" two dice and displays their values. After you read the chapter Making Decisions, you will be able to have the game determine the higher die. For now, just observe how the values change as you execute the program multiple times. Save the application as TwoDice.java.
The 2 minute drill from page 69 SCJP kathy and bert book, says regarding Interfaces, that - "A legal nonabstract implementing class must not declare any new checked exceptions for an implementation method."
When I try the below given code in eclipse , it does not throw any errors . (Here I have tried to throw NullPointerException from testFunc whereas the interface function throws IllegalStateExc)
package abstracttesting; public class StaticCheck implements check{ public void testFunc() throws NullPointerException{ // TODO Auto-generated method stub } public static void main(String[] args) { // TODO Auto-generated method stub } } interface check{ void testFunc() throws IllegalStateException; }
I'm trying to call the grade.processFile method from the main method but I'm getting this Error below. I'll post my code which includes the main method and the class underneath the error message:
Exception in thread "main" java.lang.NullPointerException at java.io.FileInputStream.<init>(FileInputStream.jav a:130) at java.util.Scanner.<init>(Scanner.java:611) at MyGrades.processFile(MyGrades.java:49) at myGradesMain.main(myGradesMain.java:19) import java.util.Scanner; import java.io.*;
I have an xml with 'n' number of data which i am parsing,for test i hardcoded without looping has below,now the below line is just parsing and showing the data for index '1' ,i need to loop this and i am not sure how can i do this.How do i find the length of obj and loop,i cannot find any method in SoapObject.I used like below but the data is getting overridden after parsing
for(int i=0;i<obj.getPropertyCount();i++) { KSoap2ResultParser.parseBusinessObject(obj.getProp erty(i).toString(), getReminder); } call in another class public static void parseBusinessObject(String input, Object output) throws NumberFormatException, IllegalArgumentException, IllegalAccessException, InstantiationException{
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!”);
why overridden doesn't apply to variables. However, instance variables are stored inside the object.I ran below program and expected to print "two" but it gets printed "one".
class SupCont { String s = "one"; } class Cont extends SupCont { public static void main(String a[]) { String s = "two"; SupCont c = new Cont(); System.out.println(c.s); } }
I am getting a nullpointer exception after "cleaning" up my code by putting repetitive stuff in a method.
The error points to this: ai.getItRight(n, answer);//make sure the user enters yes or no..
The error occurs at lines 19 and 25.Here is the relative code:
public boolean AskQuestions(Node n, String yesOrNo, String answer, String question){ if(yesOrNo.equalsIgnoreCase("no") && n.getRight() == null){//i guessed the wrong answer System.out.println("I give up. Who is it: "); answer = input.nextLine();
[code]....
My previous code, which use to have the contents of getItRight in place of lines 19 and 25 worked just fine. So why am I getting this error? I dont want my methods to be crazy big like how they usually end up.
@MappedSuperclass public abstract class BssStandardEntityLaravel extends BssStandardEntity implements InterfacciaBssStandardEntity, Cloneable{ private static final long serialVersionUID = 1L; @Column(name = "created_at", nullable=true) @Temporal(TemporalType.TIMESTAMP) protected Date created_at = new Date();
[Code] ....
When i try to read some data with a JPA controller, this error is raised:
Persistent class "com.bss.libbssabstract.database.entity.BssStandardEntityLaravel" has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class. org.datanucleus.store.rdbms.exceptions.NoTableManagedException: Persistent class "com.bss.libbssabstract.database.entity.BssStandardEntityLaravel" has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class. at org.datanucleus.store.rdbms.RDBMSStoreManager.getDatastoreClass(RDBMSStoreManager.java:702)
[Code] ....
It requires BssStandardEntityLaravel table like a normal entity. I used the same entity package in other applications and it works perfectly. Why this error is raised?
I got one task from my manager, regarding browser back button, refresh button. He asks me the web application has to work like Banks site... means if I refresh or click on Back button(Browser's) then it has to throw the user out of session, I checked lot in internet. But I found like only disabling back button of disabling F5 keys like that. But he’s not accepting that.
How to approach for this? Can we throw the user out of session when he clicks on browser back button or refresh button. I think its possible . But i don't know how to implement.
I was giving a quick skim to some tutorials on the internet where I have found the exception that is handled is also declared in the throws clause of the method wrapping the try-catch block. For a code representation consider the following:
public void methodName() throws IOException { try { ... } catch (IOException ex) { .... TODO handle exception }