Static Error And NullPointerException With Array

Nov 7, 2014

I have an arraylist, list, that I need to take the size to declare an array, arr3, in another class. Problem 1: the size is giving me an error of "can not make static reference to non-static method getListSize() from the type FindItemInfo". I tried to change getListSize() to static and it gives me and error another place. I try to then fix that, and I get a new error... and so on.

I am using arr3 to store items the user can not afford. I have this next problem even when I input an integer for the arr3 size. I am getting a NullPointerException, and I can't figure out why the arr3[] is not loading. I tried debugging but can not figure out where I went wrong, especially since this code was working in the last assignment before I changed list[] to an arrayList. It always breaks at line 64, but I believe it has to be somewhere in the cashOut() method.

Relevant code is:

public class FindItemInfo implements InterfacePrint{
ArrayList <ItemAttribute> list = new ArrayList<ItemAttribute>();
//does stuff
public void printPriority(){
TransactionCalc finish = new TransactionCalc();

[Code] .....

View Replies


ADVERTISEMENT

Getting A NullPointerException Error?

Oct 23, 2014

I am getting a NullPointerException Error and I cannot seem to figure out what is causing it. I am reading in a grades1.dat text file and putting the values into a 2D array. The error is occuring in my addGrade method, but I am not sure what the error is. Here are the values for my grades1.dat that I am running in through Run Arguments in JGRASP.

Student1
5
a Activities 0.05
q Quizzes 0.10
p Projects 0.25
e Exams 0.30
f Final 0.30
a100 a95 a100 a100 a100
q90 q80 q100 q80 q80 r90
p100 p95 p100 p85 p100
e77.5 e88
f92

Here are the errors that I am getting.

Exception in thread "main" java.lang.NullPointerException
at GradeBook.addGrade(GradeBook.java:114)
at GradeBookApp.main(GradeBookApp.java:55)
import java.util.Arrays;
/**
* Stores students name, a char array of category codes, a String array of categories, a double array of category weights, and a two dimensional double array of grades where each row contains all the grades in a particular category.

[code]....

View Replies View Related

Non-static Variable Cannot Be Referenced From Static Context - Error

Mar 15, 2015

I am trying to call an actionListener which is shown below in my PSVM :

class testMenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
getContentPane().removeAll();
createPanel();
getContentPane().add(panel1); //Adding to content pane, not to Frame
repaint();
printAll(getGraphics()); //Extort print all content

[Code] .....

I get the following error :

Frame.java:409: error: non-static variable this cannot be referenced from a static context
menuItem1.addActionListener(new testMenuItemListener());

View Replies View Related

Non-static Variable This Cannot Be Referenced From A Static Context - Error

Mar 14, 2015

I am trying to call an actionListener which is shown below in my PSVM :

class testMenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0) {
getContentPane().removeAll();
createPanel();
getContentPane().add(panel1); //Adding to content pane, not to Frame
repaint();

[Code] .....

I get the following error :

Frame.java:409: error: non-static variable this cannot be referenced from a static context
menuItem1.addActionListener(new testMenuItemListener());

View Replies View Related

Error Setting Up Object - Static Vs Non-static

Apr 3, 2014

This is the overall code:

public class Bank { 
/**
* Customer class.
*/
public class Customer {
private String firstName, lastName, street, city, state, zip;
 
[Code] .....

The error occurs when I attempt to create the new object Account within the main arguments here:

Account munozAccount = new Account(250, "Maria", "Munoz", "110 Glades Road", "Mytown", "FL", "33445");

I'm not sure how to access the generator method, so i'm completely stumped.

View Replies View Related

Java Eclipse - Public Static Int Error

Oct 28, 2014

I've got a problem in Eclipse. The below code is a part of my program, used for (re)starting a new game. The 'public static int' statement gives the error 'This method must return a result of type int'..

public static int playAgain(){
boolean validInput = false;
do {
System.out.println("Would you like to play a game? Please answer 'yes' or 'no'.");
String playAgain = input.next();

[Code] ....

View Replies View Related

Using Static Method To Convert A String To Integer Object - Compiler Error

Mar 1, 2014

I am using a static method to convert a string to an Integer object. Next using a instance method to convert Integer object to an int.

Compiler is giving me two "cannot find symbol" errors:

One pointing to the dot operator between "Integer.valueOf(s)"

The other pointing to the dot operator between "obj.intValue()"

I have latest JDK installed: jdk-7u51-windows-x64.exe

Looks like JCL installed correctly with rt.jar file located in "lib" directory under "Program Files"

Following is source code:

Java Code:

public class StringToInt
{
public static void main (String args [])
{
String s = "125";
Integer obj = Integer.valueOf(s);
int i = obj.intValue();
i += 10;
System.out.println(i);
}
} mh_sh_highlight_all('java');

View Replies View Related

Catching Array From Static Method

Apr 16, 2015

I'm writing a code with a cellphone class to set price, brand and serial number. I'm also, in the main method, initializing 100 different cellphone in a matrix style ( up to here I'm fine). I have to use a copy constructor to define some cellphones ( fine too). Another thing I had to do was to generate random numbers and swap the price of the cellphones ( which I'm fine with too). My problem lies after using my static method. I have to display a new matrix with the changed price and return the counter that I used in my method to see how many times it was changed.

My two problems are that if I display my array again after I ran the method, it stays the same ( I didn't "catch" the change so I'm guessing the compiler just didn't keep them in the array). Secondly, I don't know how to return the counter. I don't have any ".getCounter" or something ( what I'm used to). Any input?

My formatting is terrible, I'm trying to improve it no need to point it out

Here's my code:

import java.util.Random;
//Cellphone Class ( apart from main method)
class Cellphone {
private String brand;
private long serialNumber;
private double Price;
//declaration of variables in cellphone class
 
[Code] ......

View Replies View Related

Invoking Static Method With Array

Apr 16, 2015

I'm writing a code with a cellphone class to set price, brand and serial number. I'm also, in the main method, initializing 100 different cellphone in a matrix style ( up to here I'm fine). I have to use a copy constructor to define some cellphones ( fine too). Another thing I had to do was to generate random numbers and swap the price of the cellphones ( which I'm fine with too). My problem lies in my static method. I coded it all, but I can't seem to invoke it on the cellphone.

Basically, the method has to search for cellphones in the array with the same price, swap the price, print it out, and keep a counter of the price swap it has made. But I can't seem to invoke it on eclipse. It keeps telling me it is undefined for the class cellphone ( the method modifyCellPhone)

Here's the code:

import java.util.Random;
class Cellphone {
private String brand;
private long serialNumber;
private double Price;
public Cellphone (String br, long sN, double Pr) {

[Code] .....

View Replies View Related

Create A Static Array Of Objects In Java?

Jan 2, 2014

Im looking for a means to store groups of static data..

so I understand these simple string arrays...

Java Code:

private static String[] names = new String[] {
"aidanmack",
"johnsmith"
}
private static String[] ages = new String[] {
"30",
"30"
} mh_sh_highlight_all('java');

But can I not combine that into an array of objects? something along the lines of what you would do with json? like...

Java Code:

private static array[] multi = new array(){
{"name":"AIDANMACK","age":"30"},
{"name":"johnsmith","age":"31"}
} mh_sh_highlight_all('java');

View Replies View Related

Adding To Non-Static Array From Main Method

Dec 3, 2014

I believe I am on the right track but when I run the program I get an error that the class that I can't call on the method. I need to have what is selected in the JCheckBoxes added to the toppings array and I can't get that done what so ever.

Java Code:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class PizzaOrderListener
{
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();

[Code] ....

View Replies View Related

How Many Elements Array Contain Which Is Passed To Public Static Void Main In TestCommandLine

Apr 8, 2014

java com.brainbench.TestCommandLine -p Parameter1 Parameter2 Parameter3 Parameter4 ..

If the user enters the command line shown above, how many elements are contained in the array which is passed to "public static void main(String args[])" in TestCommandLine?

Choice 1 Four
Choice 2 Five
Choice 3 Six
Choice 4 Seven
Choice 5 Nine

View Replies View Related

Servlets :: Cannot Make Static Reference To Non-static Method GetQuery From Type Resource

Mar 26, 2015

This is my code inside the method:

@Post
public static String getDetails(Representation entity) throws Exception {
String customerId = getQuery().getValues("cus_id");
}

I use this code in Restlet Representation. I try to get the value from the Request API. But I am facing the problem as "Cannot make a static reference to the non-static method getQuery() from the type Resource".

View Replies View Related

Can Static Method Return Instances Of Same Class In Special Case Where Everything Is Static?

Jun 27, 2014

From what i understand static methods should be called without creating an instance of the same class . If so why would they return an instance of the same class like in the following : public static Location locateLargest(double[][] a) , the Location class being the same class where the method is defined . I don't understand this , does it mean that every field and every method in the class must be static ? Meaning that you cannot have instances of the class because everything is static . Or it's just a mistake and the class Location cannot have a static method: public static Location locateLargest(double[][] a) ?

View Replies View Related

Update User - Cannot Make Static Reference To Non-Static Method

Apr 26, 2015

I can't figure out what this error message "Cannot make a static reference to the non-static method getEndUserCharge(long, long, long, long) from the type UpdateUserWS" actually means.

The error is coming from:

public void updateDetailsPackage() {
some unrelated code
long zero=0;
double endUserCharge=0;
endUserCharge = UpdateUserWS.getEndUserCharge(long zero, long zero, long zero, long zero); <-------- error is here

[Code] ....

View Replies View Related

Player Class - Cannot Make Static Reference To Non-static Method

May 26, 2015

Alright, I have two classes, this one

public class Player {
private String player;
public String getPlayer() {
return player;
}
private int strength;
private int defense;

[Code] .....

However, it says that under Player.getPlayer() that it 'Cannot make a static reference to the non-static method'.

View Replies View Related

Instantiating Class With Non Static Variables From Within Static Method

Oct 28, 2014

Why I can create an Instance of a class that contains non static variables within the static main method ?

This program runs fine

import java.util.*;
public class Test{
public int j;
public static void main(String[] args) {
Test test1=new Test();
System.out.println(test1.j);

[Code] .....

View Replies View Related

Non-static Variable Special Cannot Be Referenced From A Static Context

Mar 3, 2015

I am trying to add a field (called special) to a hibernate table. I am copying existing code (related to the NAME field) so I don't have to figure this out from scratch. I am getting the error

"[ERROR] C:VOXvoxware-1.1.13voxwarevoxware-implsrcmainjavacomvoxwareimplflowVoxFlowConfiguration.java:[213,38] error: non-static variable special cannot be referenced from a static context".

Line 213 is in public void mergeFrom, the actual line is "special = VoxFlowConfiguration.special;" I don't understand why Java thinks special is a "non-static" variable but it doesn't have a problem with the other variables (such as name, orderShow)

package com.voxware.impl.flow;
import com.voxware.asset.LiabilityType;
import com.voxware.flow.FlowConfiguration;
import com.voxware.flow.OrderFlow;
import com.voxware.flow.Step;
import com.voxware.i18n.LanguageCodes;
import com.voxware.impl.i18n.UTF8Control;
import com.voxware.impl.persistence.BaseEntity;
import com.voxware.impl.portal.VoxPortal;

[code]....

View Replies View Related

Non-Static Method Cannot Be Called From Static Context Errors

Jul 27, 2014

I'm working on a banking program that is supposed to use 3 classes (Account-base class, CheckingAccount, and SavingsAccount) and several methods to display the banking information (ID, balance after a withdrawal and deposit, and the annual interest rate). This should be a pretty simple program, but I'm getting hung up on one portion of it. I'm getting some compiler errors, all of which deal with non-static variables being called from a static context (I'll also post these errors following the code). Up until the middle of last week, we just declared everything as static, but that's changed and I'm having trouble figuring out when to and when not to use static when declaring my methods, hence the compiler errors.

import java.util.Date;
public class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private Date dateCreated = new Date();

[Code] ....

Here are the compiler errors I am receiving:

Compilation completed. The following files were not compiled:
6 errors found:
File: C:UsersHiTechRedneckDesktopSummer II 2014Computer Programming PrincipleProgram 5CheckingAccount.java [line: 7]
Error: non-static method getId() cannot be referenced from a static context

[Code] .....

View Replies View Related

Cannot Make Static Reference To Non-static Type String

Apr 27, 2014

I am writing the following program in Java SE 7. It throwing "Cannot make a static reference to the non-static type String" . However if I write parameterised String inside main method as  java.lang.String[] args, it compiles fine.
 
class MainClass<String> {
  <T> MainClass(T t) {
   System.out.println(t.getClass().getName());
  } 
  public static void main(String[] args) {
  System.out.println("asdasd");
   new MainClass<>("");
  }
}
 
I mean following programs compile fine in Java SE 7 :
 
class MainClass<String> {
  <T> MainClass(T t) {
   System.out.println(t.getClass().getName());
  }
  public static void main(java.lang.String[] args) {
  System.out.println("asdasd");
   new MainClass<>("");
  }
}

View Replies View Related

Can't Call Non-static Methods Inside Static

Aug 19, 2014

i am trying to make something, and i want to request input from user and it shoud look like this

1. xxx -> press 1 to choose this
2. xxx -> press 2 to choose this

So if they enter 3 or string or whatever i want to restart method and show again

1. xxx -> press 1 to choose this
2. xxx -> press 2 to choose this

So here is method that i am trying to restart

public static void askUser(){
System.out.println("xxx");
System.out.println("1. xxx");
System.out.println("2. xxx");
System.out.println("3. xxx");
 
[code]....

If i try to make it public void than it say can't call non-static methods inside static(main).if i try to put it into new class and then call it after i fail input it goes into infinite loop.

View Replies View Related

Non-Static Variable TAShaReport Cannot Referenced From Static

Sep 28, 2014

The error said : Non Static Variable TAShaReport Cannot referenced from a static context

I just want to put the output in the TextArea

Here is the code :

public static String DeduplicateFiles(String myFolderLocation) {
try {
HashSet<String> newset = new HashSet<>();
File folder = new File(myFolderLocation); //Directory where the files are located
File[] listOfFiles = folder.listFiles();

[Code] .....

View Replies View Related

How To Have Static Or Non-static Method Reference As Parameter

Nov 18, 2014

I had a TestColor class which contained methods to change hue, saturation, brightness, red, green, blue of TestColor's instances but also had static methods which take in an additional parameter for an instance of TestColor and returns the affected instance of TestColor. Now instead of having one method for every possible color effect to be applied to an image, how can I have one method that takes in an Image parameter, a static or non-static method reference from TestColor parameter and lastly an intensnity value parameter. This is so that I can make an affectedImage object instance inside the method and a Graphics2D object for drawing to each pixel of the new image, now I have one for loop and one nested for loop for the x and y pixels of width and height of the old image and inside the nested for loop I'd create a TestColor by calling getRGB on the image's pixel. Then I would apply the static or non-static method reference somehow to change the color with the intensnity value and after applying it draw to the new Image with Graphics2D. How to would I parametize a method reference and be able to use it in such way?

View Replies View Related

Using Variable From Static Extended Class In Non-Static

Nov 18, 2014

This is a someway special question, because I am using jmonkeyEngine.

But the topic is simple:

I have 2 classes:

public class Spielbrett extends SimpleApplication {
public static void main(String[] args) {
Spielbrett app = new Spielbrett();
app.start();
}
@Override
public void simpleInitApp() {

[Code]...

as the main class and a second class for the chips:

public class Spielstein {
public Spatial stone;
public int player;
public int team;
private AssetManager assetManager = Spielstein.getAM(); //THIS IS THE PROBLEM
public Spielstein(int t_player, int t_team){

[Code]...

My problem is: I can't access getAM() from the first in the second class. If you know why I would be glad for an answer.

View Replies View Related

Which Will Be Loaded First Static Variable Or Static Block

Jan 26, 2014

One of my friend asked me that which will load first static variable or static block ? My answer was to static variable.. So he gave me two program and said to differentiate between them

1st program

public class Test {
public static void main(String args[])
{
System.out.println(Test.x);
}
static {
System.out.println(Test.x);

[Code] ....

Output of this :

90
90

I tried to decompile the byte code and found it's same for both the above equation. How to differentiate between them. I am confused when the static variable will initialised.

View Replies View Related

Threads Calling Static And Non-static Method

Jul 14, 2014

One class having two method one as static n another as non-static, 2 threads are there t1 is accessing the static method and t2 the non-static method is it possible n both are sharing the same object.

I now we have two kinds of lock one is object level lock and another is class level lock

View Replies View Related







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