Custom Exception Classes And Array

Jul 20, 2014

I've been trying to work with custom Exception classes, but I keep running into what I think is an array error. It's in a very monolithic format because I was just trying to bang it out and get it done. Anyway, my issues is I am trying to compare values in the array to the minimum and maximum possible scores for a student (0 and 100) but I have totally forgotten how to do it.

Here is the code, the offending bit is at the very bottom:

package org.CIS407.Lab6;
import java.util.*;
public class TestScore {
public static void main(String[] args) throws ScoreException {
Scanner scan = new Scanner(System.in);
int sz; //holds scanner values
int[] studentArray;

[Code] .....

Right now I'm getting an error when I go to enter the very last student score. It throws an exception.

Here is a sample output of what I'm getting:

Enter number of students
2
Array created successfully. Enter student ID's into array
1
2
Enter number of scores
2
Array created successfully. Enter student scores into the array
50
32

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at org.CIS407.Lab6.TestScore.main(TestScore.java:64)

View Replies


ADVERTISEMENT

Custom Jtable Cell Editor Throwing Illegal Component State Exception

Jul 10, 2014

I've come across an interesting problem when using a Jcombobox as a custom cell editor(and renderer) in a jtable. I was hoping to add a keybinding in order to display the dropdown of the combobox instead of having to click on it however, when I make a call to showPopup() I get:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location Which is strange as my jtable is visible and the editor/renderer seems to be working fine.

Here's the cell editor + renderer I'm using:

class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
public MyComboBoxRenderer(String[] items) {
super(items);
} public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());

[code]....

View Replies View Related

Creating Three Exception Classes

Sep 28, 2014

Why I have to place two parameters in the Verify constructor. If the numbers are being typed in by the user and passed through the validate method, what is the point of having them in Verify() ?

First, create three exception classes named NumberHighException, NumberLowException, and NumberNegativeException. Both NumberHighException and NumberLowException should be directly subclassed from the Exception class, but NumberNegativeException should be subclassed from NumberLowException. You can use the BadDataException class that was defined in this module as the model for your exception classes.

Next create a class called Verify that will be used to validate that a number is within a specified range. It should have one constructor that has two int parameters. The first parameter is the minimum number in the range, and the second parameter is the maximum number in the range.

In addition to the constructor, the Verify class should have one method that is named validate. The validate method should have a single parameter of data type int. The parameter contains the number that is being validated. If the value of the parameter is less than zero, the method should throw a NumberNegativeException. If the value is less than the minimum value of the range, it should throw a NumberLowException. If the value is greater than the maximum value of the range, it should throw a NumberHighException. If the value is within the specified range, no exception should be thrown.

Once all of these classes are created, create the driver class called Program5. The driver class should instantiate a Verify object with a range of 10 to 100. It should then do the following:

-Prompt the user to input a number within the specified range.
-Use a Scanner to read the user input as an int. You can ensure that an int was entered because the nextInt method throws an --InputMismatchException if any non-digits are entered.
-Call the validate method to validate that the number is within the range.
-Print an appropriate error message if the value is not within the range, or print the value if it is within the range.

View Replies View Related

Thread And Passing Exception With Two Classes

Jan 23, 2014

I have two classes, where main class is simple with only main function in it. Another class extends Thread and there's couple of functions I want to execute from main class.

Problem: I try to get other.shut() to be run in main class catch() block, after I have stopped the other class's running thread (e.g. by ctrl+c).

I think I need to somehow "pass" the exception from other class to main class so it goes to the catch block?

Code for main class:

Java Code:

public class MainClass {
public static void main(String[] arguments) {
OtherClass other = new OtherClass();
try {
other.exec();
} catch (Exception e) {
other.shut();

[Code] .....

View Replies View Related

Eclipse IDE - 2 Classes And Exception In Thread Main

Aug 31, 2014

I use Eclipse IDE

public class Emp {
 private String name;
private String jobTitle;
 public void setName(String nameIn){
name=nameIn;

[Code] ....

<terminated> TestClass
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at TestClass.payOneEmp(TestClass.java:21)
at TestClass.main(TestClass.java:13)

View Replies View Related

Array Index OutOfBounds Exception While Moving Creature Through 2D Array

Oct 13, 2014

I am receiving an ArrayIndexOutOfBoundsException for the following code, which moves a creature through a 2D array maze. I have altered the clauses of the first if statement for the four direct methods (north, south, east, and west) multiple times (i.e. x + 1 >= 0 && x > 0 && x - 1 > 0 && x < array.length...etc). However, while the code occasionally runs, more often than that it returns this exception. Catching the exception seems like a poor workaround though if worst comes to worst I'll do that.

I included only the relevant functions of the code:

public boolean goNorth(char[][] array) {
boolean success = true;;
x = getX();
//x = this.x;
y = getY();
//y = this.y;
if ((x - 1 >= 0 && x - 1 < array.length)
&& (y >= 0 && y < array[x].length)) {

[Code] .....

View Replies View Related

Interface / Abstract Classes And Loading Array

Oct 20, 2014

I am new to Java, and last week had an assignment to create a shopping list. I made it so that I have one class use a ProductData class to load an array of objects (description, price, priority). This week I need to take that program and change it so that it includes an Interface and Abstract Class. I need to also split one class up into at least 2 others.

I am having trouble getting my thoughts together and figuring out what to put in the interface and what to put in the abstract class. I'm thinking that it might be best to split up the ProductData class up into 3 different classes: description, price, and priority. Then have an interface with a print method. Each of those 3 classes will implement the interface.

As for the abstract class, have the price and priority extend the abstract class. The abstract class will be at the same level as the interface and contain the set and get methods. Right now they are of 2 different data types: int, double. Should I make both of them Double, and then use a method to change the priority to an int?

Should price and priority inherit from description, or should they all be at the same level? I am thinking that they should be at the same level because they all describe the item in the array.

My most confusing part is that I have no clue at all on how I can load that array when each object is split up in a different class. My professor went over ArayLists last week, and we can now use them if we want, but the assignment doesn't explicitly say that we should change it to an Array List. Where does the constructor for the ProductData() go? Do I split it up into 3 different constructors?

View Replies View Related

Storing Custom Object In SQLite DB / Serializing Custom Object?

Jul 13, 2014

I have this arraylist off a custom object that looks like this witch also contains a list off custom objects

package holders;
import java.util.ArrayList;
public class ManufacturingJobHolder {
private String name;

[code]....

View Replies View Related

Exception With Array Of Integer

Mar 3, 2014

I'm trying to insert numbers in a array of Integer neatly. When i try to insert the following numbers i have this exception.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at time.Historico.insere_ordenado(Historico.java:35)

Line 35 in my code is:
"while (num >= arry[i] && arry[i] != null && i < arry.length - 1)"

Numbers:
71516357248031

I have this exception when i try to instert 80.

public Integer[] insert_neatly(Integer[] arry, Integer num)
{
Integer i, acum, aux, temp;
i = acum = 0;
if (arry[i] == null)

[Code] ....

What is wrong with this code?

View Replies View Related

Array Index Out Of Bounds Exception

Feb 18, 2009

I am getting Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at lsb2.main(lsb2.java:44)Here is the code:

Java Code: import java.io.*;
import java.lang.Integer.*;
import java.lang.Math.*;
import java.util.*;
class lsb2

[code]....

View Replies View Related

Array Index Out Of Bounds Exception?

Jan 28, 2014

java.lang.ArrayIndexOutOfBoundsException: 991

at Champion.main(Champions (1) (1).java:209)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)

[Code]....

View Replies View Related

Why Getting Array Index OutOfBounds Exception

Oct 13, 2014

I am trying to make different arrays each being filled with random numbers from 0 to 2000, however when I run this code I get the error mentioned in the header. here is part of my code

for (int i = 1; i <= 14; i++) {
int n = (int) Math.pow(2, i);
int[] list = new int[n];
for( int j = 0; j <= list.length; j++){
list[j] = (int) (Math.random() * 2000);
}
}

View Replies View Related

Airline Having With Array Index Out Of Bounds Exception?

Mar 22, 2014

class test{//class
public static void main(String[]args) {
String booking [][]= new String [30] [6] ;//two dimensional array
System.out.println("Enter the seat column you want");//column entry
char column=Keyboard.readChar();
System.out.println("Enter the seat row you want");//row entry
int row=Keyboard.readInt();

[code]...

cant get my in put to go in to the index

View Replies View Related

Bubble Sort - Array Out Of Bounds Exception

Jan 10, 2015

I am having trouble with an array out of bounds exception. I understand what the error means (that I am trying to access part of array that does not exist). But I do not see the error in my code.

Java Code:

public class bubbleSort
{
public static void main(String []args)
{
int i;
int array [] = {12,9,4,99,120,1,3,10};

[code]....

View Replies View Related

Sort2D - Array Index Out Of Bound Exception

Dec 15, 2014

When I am trying to execute this code it is showing

Java Code:

class Sort2D
{
public static void main(String...s) {
Sort2D r=new Sort2D();
int z[][]=r.sort(new int[][]{{1,0,567,4,3,33},{333,677,34243,8987,3434,324},{446,876,23,546565,332}});
for(int i=0;i<z.length;i++)
for(int j=0;j<z[i].length;j++)
System.out.println(z[i][j]);

[Code] ....

View Replies View Related

Exception In Thread - Array Index Out Of Bounds

Mar 20, 2015

The following code generates an array out of bounds exception

int[] myArray1 = new int[5]; 
for(int j = 0; j < myArray1.length; j++)
{
myArray1[j] = 10;
System.out.println("myArray1["+j+"] : " + myArray1[j]);
}
for (int i : myArray1) {
System.out.println("Blah myArray1["+i+"] : " + myArray1[i]);
}

The exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Tutorial2.main(Tutorial2.java:40)

View Replies View Related

Array Index Out Of Bounds Exception Error

Jan 24, 2015

I have two codes of needed to run but after processing, the said above error is preventing it from getting the general output of the code.I can't seem to find my errors or I don't have any clue at all, if perhaps you know, here's the codes:

Error :
PHP Code:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at simpleOutput.main(simpleOutput.java:13) 
public class simpleOutput {
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
for(int i = 1; i <= n; i++){
}
for(int i = 1; n <= i; n++){
System.out.print((i*n)+" ");

[code]....

View Replies View Related

NullPointer Exception From Calling A Setter In Array Of Objects

Sep 21, 2014

I hope I'm putting this question in the right folder. I have an array of objects, and I have defined a setter for a variable in the object. When I call the setter, I get a NullPointerException. Here is the relevant code for the object.

public class Digit extends Thread {
private int digit;
public void setDigit(int digit) {
this.digit = digit;
}
// run method follows
}

Here is the portion of the main class where I define an array and then call the setter.

Digit[] digits = new Digit[10];
for (int i = 0; i < digits.length; i++) {
digits[i].setDigit(i); // NullPointerException occurs here
}

View Replies View Related

Java Array Throws Exception For Multiple Values

Jan 3, 2014

int nRows = 0;
        for (EITest testCurr : testList) {
            testCaseListArray[nRows] = new Object[3];
            String testName = testCurr.getTestName();
            LOG.info("testName=" + testName);
            testCaseListArray[nRows][0] = testName;

[Code] ....

This throws null pointer exception because of array initialization error. I tried this:
 
int nRows=0;
        for(EITest testCurr: testList){          
            testCaseListArray[nRows] = new Object[3];
            String testName = testCurr.getTestName();
            LOG.info("testName=" + testName);
            testCaseListArray[nRows][0] = testName;

[Code] ....

But this is not complete solution.
 
2) I want to make this as a collection object such as array list instead of Array declaration.

View Replies View Related

Reading From Text File - Array Index Out Of Bounds Exception

Oct 21, 2014

I need to read from a text file given to us that has a list of books with authors names and book titles separated by an @ symbol for our delimiter. Here is the code I have right now, but it throws an ArrayIndexOutOfBoundsException at line 7...and I am unsure why?

import java.io.*;
import java.util.*;
public class Driver {
public static void main(String[] args) {
new Driver(args[0]);

[Code] ....

I realize that it must have something to do with my command line argument...but I am unsure what. Am I not entering the file name correctly?

View Replies View Related

Removing Item From String Array - Null Pointer Exception Error

Jun 8, 2014

Question 1: I am working on an assignment where I have to remove an item from a String array (see code below). When I try to remove an item after entering it I get the following error "java.lang.NullPointerException." I am not sure how to correct this error.

Question 2: In addition, I am having trouble figuring out how to count the number of occurrences of each string in the array and print the counts. I've been looking at other posts but they are more advanced and I have not yet learned how to use some of the tools they are referring to.

private void removeFlower(String flowerPack[]) {
// TODO: Remove a flower that is specified by the user
Scanner input=new Scanner(System.in);
System.out.println();
System.out.println("Please enter the name of the flower you would like to remove:

[Code] ....

View Replies View Related

What Handling Exception And Declaring Same Exception In Throws Clause Achieve

Jan 24, 2014

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
}

}

View Replies View Related

How To Call Classes Within Other Classes

Apr 18, 2014

How do you call classes within other classes? Or can you only call classes through the main?

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

Passing Values To Two Classes And Retrieving Values From Those Classes

Feb 14, 2015

I'm doing an aggregation exercise that's suppose to find the volume and surface area of a cylinder. What I'm trying to do is pass values from one class, to a second class, and that second class passes values to a third class.

This may be a clearer explanation: The first class is the main program which sends values to the second and third class. The second class is used do calculations for a circle (a pre-existing class from another assignment). The third class grabs the values that the second class calculated and calculates those values with the one that was passed from the first class to the third class. The first class then prints the outcome.

Problem is when the program gets to the third class, it just calculates the value from the first class with the default constructor from the second class. It's like the second class never received the values from the first class. I think I'm missing a step, but I don't what it is.

First Class:

package circle;
import java.util.Scanner;
public class CylinderInput
{
static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
//user defined variable

[Code]...

View Replies View Related

JSP :: Custom Tag Not Called At All

Jun 30, 2014

I am working on a project that uses a custom tag in a JSP file. Here is the tag descriptor

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0.0</tlibversion>
<jspversion>1.1</jspversion>

[code]...

Everything seems working fine except that the custom tag handler does not get called at call. I set breakpoint and it never stops there. The JSP page is displayed fine, only not showing the custom tag content and there is no logged error/warning from the log. The tld is found fine. Like what classes called/validated the tag handler so I can logging those classes at DEBUG level to see what goes wrong.

View Replies View Related







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