This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I have made five classes (as shown below). When I run my code it gives me "NullPointerException"
First class
public class PasPosition {
boolean lessThanThirty;
boolean ThirtyToFifty;
boolean FiftyToHundred;
public PasPosition(boolean value1, boolean value2, boolean value3)
{
lessThanThirty = value1;
ThirtyToFifty = value2;
FiftyToHundred = value3;
}
}
Second Class
public class CssPosition {
boolean lessThanThirty;
boolean ThirtyToFifty;
boolean FiftyToHundred;
public CssPosition(boolean value1,boolean value2, boolean value3)
{
lessThanThirty = value1;
ThirtyToFifty = value2;
FiftyToHundred = value3;
}
}
Third class
public class SiteData {
PasPosition pas;
CssPosition css;
}
last class
public class Test {
SiteData[] sitedata = new SiteData[2];
public void test()
{
for(int i=0;i<sitedata.length;i++)
{
System.out.println(sitedata[i].css.FiftyToHundred);
System.out.println(sitedata[i].css.ThirtyToFifty);
System.out.println(sitedata[i].css.lessThanThirty);
System.out.println();
}
}
}
It seems that you didn't init the objects.
You must know that in java, an object in a class will not be automatically inited. You must use new operator.
public class SiteData {
PasPosition pas=new PasPosition(false,false,false);
CssPosition css=new CssPosition(false,false,false);//use your own initial value
}
Related
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 1 year ago.
could someone explain this piece of code. The output is where I am confused. It seems only one thing (i.e. p) was modified by the modifyObject function, but the other (i.e. s) is left unchanged. But I am confused. Could someone explains what's going on.
class Person{
int a = 8;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
#Override
public String toString() {
return "Person [a=" + a + "]";
}
}
public class TestMutable {
public static void main(String[] args)
{
Person p = new Person();
p.setA(34);
String s = "bar";
modifyObject(s, p); //Call to modify objects
System.out.println(s);
System.out.println(p);
}
private static void modifyObject(String str, Person p)
{
str = "foo";
p.setA(45);
}
}
why is it that the following is output? i.e. str is still bar , but person.A is now 45?
bar
Person [a=45]
You can see this thread to understand why String value doesn't change and how to make it to change it: Passing a String by Reference in Java?
This question already has answers here:
How to write a Unit Test?
(5 answers)
Closed 4 years ago.
I have a problem with Mockito. I have two different class. My purpose is test “setChanges” function. this is my first class :
class M {
private String a;
private String b;
private boolean c = false;
public String getA() {
return a;
}
public void setA( String _a ) {
a = _a;
}
public String getC() {
return c;
}
public void setC( final boolean imp ) {
c = imp;
}
}
this is the main class which has “setChanges” function:
class MyMainClass {
private String getMyA() {
return "Data";
}
private static void setChanges(final M m) {
if (getMyA().equals(m.getA())){
m.setC(true);
}
}
}
How can I test "setChanges"? Which means that if getA() returns "Data", How can I check getC() that should be "true"?
Thanks, It works with this code :
#Test
public void testsetChanges(){
MyMainClass mmc = new MyMainClass ();
M m = new M();
m.setA("Data");
Method method = MyMainClass.class.getDeclaredMethod(
"setChanges",
M.class
);
method.setAccessible(true);
method.invoke(method, m );
assertTrue(m.getC());
}
Pass in an instance of M which satisfies (or doesn't satisfy) getMyA and validate that M#getC returns true (or false, depending on what you're testing). No mocks required.
This question already has answers here:
How to find an object in an ArrayList by property
(8 answers)
Closed 4 years ago.
I am maintaining a sorted ArrayList of objects (by overwriting the add method as shown here) where each object has 2 attributes: a and b. How can I retrieve an object for which a equals 5?
I cannot use a map, because the value which I want to sort the list on must be able to accept duplicates (which is why this answer is not applicable here).
Code:
class TimeMap {
List<MyType> list = new ArrayList<KVT>() {
public boolean add(KVT mt) {
int index = Collections.binarySearch(this, mt, new SortByTime());
if (index < 0) index = ~index;
super.add(index, mt);
return true;
}
};
}
class KVT{//value-timestamp object
String value;
int timestamp;
public VT(String v, int t){
value=v;
timestamp=t;
}
}
class SortByTimestamp implements Comparator<KVT>{
public int compare(KVT a, KVT b){
return a.timestamp.compareTo(b.timestamp);
}
}
I have written a small example using java8 streams where you can get the object from the ArrayList by a property of the object.
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Test> list = Arrays.asList(new Test(1, 2), new Test(5, 6), new Test(3, 4));
Test test = list.stream().filter(obj -> obj.a == 5).findFirst().orElse(null);
System.out.println(test.a);
}
}
class Test {
int a;
int b;
Test(int a, int b) {
this.a = a;
this.b = b;
}
}
Hope this will give you an idea
Here is an mcve demonstrating retrieval by timestamp as well as some other enhancement:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TimeMap {
private List<KVT> list;
TimeMap() {
list = new ArrayList<>() {
#Override
public boolean add(KVT mt) {
super.add(mt); //add
Collections.sort(this, new SortByTimestamp()); //resort after add
return true;
}
};
}
boolean add(KVT mt){return list.add(mt);}
KVT getByTimeStamp(int timestamp){
for(KVT mt : list){
if(timestamp == mt.timestamp)
return mt;
}
return null;
}
//returns a copy of list
List<KVT> getListCopy() { return new ArrayList<>(list) ;};
//test
public static void main(String[] args) {
TimeMap tm = new TimeMap();
tm.add(new KVT("A", 2));
tm.add(new KVT("B", -3));
tm.add(new KVT("C", 1));
System.out.println(tm.getListCopy());
System.out.println(tm.getByTimeStamp(1));
}
}
class KVT{
String value;
int timestamp;
public KVT(String v, int t){
value=v;
timestamp=t;
}
#Override
public String toString(){ return value+" ("+timestamp+")";}
//todo add getters
}
class SortByTimestamp implements Comparator<KVT>{
#Override
public int compare(KVT a, KVT b){
//compareTo can not be applied to primitives
return Integer.valueOf(a.timestamp).compareTo(b.timestamp);
}
}
This question already has answers here:
cannot make a static reference to the non-static field
(7 answers)
Closed 7 years ago.
When I am calling a method directly from main method, it is not allowed. However, when I am calling the same method from the constructor of a class, it is allowed.
The allowed version;
public class App {
Integer firstVariable;
Integer secondVariable;
public static void main(String[] args) {
App obj = new App(3, 2);
}
public App(Integer firstVariable, Integer secondVariable) {
this.firstVariable = firstVariable;
this.secondVariable = secondVariable;
this.calculate(firstVariable, secondVariable);
}
public int calculate(int a, int b) {
int result = a + b;
return result;
}
}
The disallowed version;
public class App {
Integer firstVariable;
Integer secondVariable;
public static void main(String[] args) {
App obj = new App(3, 2);
obj.calculate(firstVariable, secondVariable);
}
public App(Integer firstVariable, Integer secondVariable) {
this.firstVariable = firstVariable;
this.secondVariable = secondVariable;
}
public int calculate(int a, int b) {
int result = a + b;
return result;
}
}
I know it is "Cannot make a static reference to the non-static field firstVariable" error. My question is; In both code blocks, the same thing is done but what is the difference between them?
The issue isn't your method. The issue is that your variables (the arguments that you're trying to pass) are being referenced from a static context.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I have a homework about implementing queues in java. I have written a code, but there is an error and I don't know how to fix it. Can please anyone help me with this?
Here is my code:
public class Radha {
public int num;
public Radha pas;
public Radha(int num){
this.num = num;
}
public void shfaq(){
System.out.println(num);
}
public static void main (String [] args){
Radha x = new Radha(1);
Radhe1 r = x.new Radhe1();
r.enqueue(1);
r.shfaq();
}
class Radhe1{
public Radha koka;
public Radha bishti;
Radhe1(){
koka.pas = null;
bishti = koka;
}
public void shfaq(){
Radha theLink = koka;
while(theLink != bishti){
theLink.shfaq();
theLink = theLink.pas;
}
}
public boolean bosh(){
return(bishti == koka);
}
public int iPari (){
if (bosh())
System.out.println("radha eshte bosh");
return(koka.num);
}
public void dequeue(){
if (bosh()){
System.out.println("radha eshte bosh");
}
else{
koka = koka.pas;
}
}
public void enqueue(int a){
bishti = bishti.pas;
bishti.num = a;
bishti.pas = null;
}
}
}
When you write koka.pas = null, there is no koka whose pas you can set. You must initialize that somehow.