Im calling DataComparison()
public class SteganographyGUI {
...
DataComparison dataComp;
dataComp = new DataComparison();
}
public int getLSB(){
String x = fileChooser1.getSelectedFile().getAbsolutePath();
x = x.substring(x.length() - 10, x.length() - 9);
return Integer.parseInt(x);
}
when some criteria are met. My problem is that, when I try to access getLSB by using gui.getLSB()
public class DataComparison {
public static SteganographyGUI gui;
...
public DataComparison(){
lsb = gui.getLSB();
}
public static void main(String[] args) {
gui = new SteganographyGUI();
gui.setVisible(true);
}
Error appears - Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
How can I fix this?
You are trying to call getLSB() in your DataComparison class, but you don't give it the reference of your SteganographyGUI class. So change the following line:
DataComparison dataComp;
dataComp = new DataComparison();
to:
DataComparison dataComp;
dataComp = new DataComparison(this);
And change the constructor as well:
public DataComparison(SteganographyGUI guiRef){
gui = guiRef;
}
You are doing cyclic calls in your code. Try another class just for the main and call its methods in order:
class 1: `public class SteganographyGUI {...}`
class 2: `public class DataComparison {...}
class 3:
`public class XXXX {
public static void main(String[] args) {
gui = new SteganographyGUI();
gui.setVisible(true);
}
}`
I have helped :)
When you try to initiate static "gui" variable
gui = new SteganographyGUI();
you execute SteganographyGUI class constructor which (probably) calls DataComparison class constructor
public DataComparison(){
lsb = gui.getLSB();
}
which uses static "gui" variable which is not set yet. This is the reason you got "java.lang.NullPointerException".
Yes, I know "the exception message is misleading" :)
Related
I cant understand whats wrong with my code. In this code i getting errors like this when im trying to assign a value to a variable within a class. Also System.out.println doesnt work in this classes:
1.Identifier expected
2.Unexpected token
3.Unknown class "windows"
public static void main(String[] args) {
}
class building{
int apart_num;
apart_num = 3;
}
class apartments{
double area;
int lightbulb;
int windows;
windows = 4;
}
interface construct_building{
}
interface construct_apartments{
}
You are declaring an instance member on one line :
int apart_num;
and then assign a value to it :
apart_num = 3;
The problem is that this is done outside a method, those statement can't be separated, you can't assign a variable previously declare outside a block statement so.
Do it in a one line (declaration and assignation) :
class building{
int apart_num = 3;
}
or with a constructor
class building{
int apart_num;
public building(){
apart_num = 3;
}
}
or in a block statement
int windows;
{ //a block statement
windows = 4;
}
Then, if this code is not a class, you need to do it.
public MyClass{
public static void main(String[] args){ ... }
...
class Building { //inner class (will exist only inside of a MyClass instance
}
static class Apartment { // a nested class, exist whitout a MyClass instance
}
}
class Level { //A class that have nothing to do with MyClass and that can not be public.
}
Where MyClass is the name of the file (MyClass.java)
I have a java code with the structure that is shown below:
public class x{
public static void main(string[] args)
{
ysample1 = new y(m)
ysample2 = new y(l)
....
}
}
public class y{
private int m_m
public y(int m)
{
m_m = m
}
public void control()
{
h h1 = new h(ysample2)
}
}
At some point when I want to call method control for ysample1 I may need to access ysample2 object.How can I define instance of class y global, so I can access ysample2 inside the control method in class y?
Does anyone know how can I fix this? Thanks.
You can't do what you want to do the way you wrote it.
I think you need to ridefine "control()" method like this:
public void control(Y ysample)
{
h h1 = new h(ysample)
}
So now you need to have an "ysample" as parameter and you can do from your main
control(ysample2);
and you will have what i understood from you question. If you need something else please comment.
I'm learning to code java and I encountered some problems in which I could use help understanding how things work.
I've made a list containing "Images", on my Main class, called "myList".
public class Main{
public static void main(String[] args) {
List<Images> myList = new ArrayList<Images>();
...
And I want to access it on this "System" class. But it doesn't seem to let me.
The plan is to access a position (the 3rd, in this example) on the given list (list.get(2)).
So I created the method "work".
//Example
public class System{
public static boolean work(List<Images> list){
if( list.get(2).equals(Something) )
return false;
else ... return true;
}
On this same System class I'm trying to use the method "work", giving it the List that I created on my Main class (myList).
public class System{
...
if( work(myList) ) //Don't know how to reffer to myList
return something;
Gives me the error "myList cannot be resolved to a variable".
So the problem is how to reffer to the list I made on my Main, named "myList".
Not sure if I explained that too well but any suggestions?
Make a List a property of System class, then pass it in the constructor
public class System {
private List<Images> images;
public System(List<Images> images) {
this.images = images;
}
//your other methods
}
Ah, in your main you should also pass the list:
System system = new System(myList);
Another option its to make myList public static and access it like this:
Main.myList
Declare one helper class and declare your list with setter and getters. Mainatin a singleton object of this class and use that list then in different other classes.
you need to make sure its accessible.
Right now your list is scoped the main() function. which is static to boot.
You need to make it accessible. You can do this by storing it in a static variable and having a static function return it.
Or you pass the main object along to other object, so they can access it.
public class Main {
private List<Images> myList = new ArrayList<Images>();
public static void main(String[] args) {
new Main(args);
}
public Main(String[] args) {
myList.add('foo.png');
myList.add('bar.png');
System mySystem = new System(this);
}
public List<Images> getImages() {
return myList();
}
}
public class System{
Main global;
public System(Main main) {
global = main;
}
public void doSomething() {
Iterator<Images> it = global.getImages().iterator();
while(it.hasNext()) {
Images images = it.next();
}
}
}
I am using the BlueJ IDE. I have a main class entitled ProgramOne, and another class StarTurtle (intended to serve an instance method).
Here is the code of ProgramOne:
public class ProgramOne
{
public static void main (String[ ] args) {
StarTurtle turtle1 = new StarTurtle();
int result = turtle1.StartTurtle(5);
}
}
Here is the code of StarTurtle:
public class StarTurtle
{
private int points;
public int StartTurtle(int x)
{
points = x;
Turtle sue;
sue = new Turtle();
sue.paint (90, 40);
}
}
(The turtle method you see is from two other classes that I have not pasted here for the sake of brevity. These classes are found in the http://www.cs.ccsu.edu/~jones/book.htm manual)
The code only compiles, and there is no option to execute. However, there is no option to execute void main (String[ ] args), which there should be to execute the main class. Does anyone know what is the cause of this? I am assuming that there is a problem in the code itself. The StarTurtle class does execute, but the main class ProgramOne does not, which leads me to believe that the problem lies in the ProgramOne class.
When I mean "option to execute", I am referring to this BlueJ functionality:
When you call the main method from the class ProgramOne, you have created an instance of StarTurtle turtle1. But when you assign value in result variable by calling turtle1.StartTurtle(5) method, nothing is stored in that variable. The problem is that you have issue in this function in your StarTurtle class. You have defined public int StartTurtle(int x) function as return type but actually it is not returning any thing. Therefore, you need to add a return statement on that block of code.
public class StarTurtle {
private int points;
public int StartTurtle(int x){
points = x;
Turtle sue;
sue = new Turtle();
sue.paint (90, 40);
return points;
}
}
However, on the other hand, your class and functions are incorrect though they are working. You classes should be like this.
public class ProgramOne{
public static void main (String[ ] args) {
StarTurtle turtle1 = new StarTurtle();
turtle1.StartTurtle();
}
}
public class StarTurtle {
//Void type of method instead of previous return type
public void StartTurtle(){
Turtle sue;
sue = new Turtle();
sue.paint (900, 550);
}
}
There's something I'm missing here. with this code I get a java.lang.NullPointerException:
public static void main(String[] args) {
Board board = new Board();
board.Initialise();
}
public class Board {
private Obj[][] tableau;
public void Board() {
tableau = new Obj[8][8];
}
public void Fill_Board() {
tableau[0][0]= new Obj('B');
}
}
But with this other code I get no error. What I am doing wrong, and how to initialize properly this array of object?
public class Board {
private Obj[][] tableau = new Obj[8][8];
public void Board() {
}
public void Fill_Board() {
tableau[0][0]= new Obj('B');
}
}
Board() ends up being a member function and not a constructor, and therefore never gets called. The problem is the void keyword, which needs to be removed:
public Board() { /* removed the `void' */
tableau = new Obj[8][8];
}
I get no error when I run this code (changing Obj to an actual class), perhaps you can provide a more concrete example with a main method of what you're trying to do?
If you're looking at making a constructor then it needs to be the same name as your class, i.e. Tab and have no return type.
So you would need to add:
public Tab() {
// initialization code here
}
You're constructor will run whenever you create a new instance of that class. You want to use it to initialize all of your variables.
Tab t = new Tab(); // Constructor runs
Edit:
You're main method uses class Board but you have given us a class called Tab. I can assume that you really want class Board so you should change all Tab to Board in the above example, if that's what you're looking for.