Why StringBuilder throws NullPointerException [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
This question may be redundant but I really don't understand why the following code throws: Exception in thread "main" java.lang.NullPointerException
public class NewClass {
static StringBuilder SB;
public static void main(String[] args) {
SB.append("Tesing");
System.out.println(SB);
}
}

SB = new StringBuilder();
You missed this part!

You haven't assigned the SB (shouldn't be capitalised btw) variable, so it is still null when you attempt call a method on it.
public class NewClass {
// Assignment added below
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
sb.append("Tesing");
System.out.println(sb);
}
}

You didn't initialize your StringBuilder. Change it to
static StringBuilder SB = new StringBuilder(); and it should work.

Related

Append Not working in StringBuilder in Java It says "method append(char) is undefined for the type StringBuilder" [duplicate]

This question already has answers here:
print current date in java
(9 answers)
Method undefined for type Java
(1 answer)
Closed 6 months ago.
public class StringBuilder {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for(char ch='a'; ch<='z'; ch++){
sb.append(ch);
}
System.out.println(sb);
}
}

Why I can't printout value from main method? [duplicate]

This question already has answers here:
Calling Non-Static Method In Static Method In Java [duplicate]
(14 answers)
Closed 2 years ago.
//I'd like to now why I can;t printout the return value in below java code
public class client08 {
public static void main(String[] args) {
String w=test();
System.out.println(w);**// I'd like to print out the return value here but it now work**
}
public String test() {
String result="";
String[] words=new String[5];
words[0]="Amy";
words[2]="Tom";
words[4]="Jane";
for(int i=0; i<words.length;i++) {
if(words[i]!=null) {
result=words[i].toUpperCase();
}else {
result="null";
}
}
return result;
}
First of all class names should start with an upper case character. Learn and follow Java conventions.
Next:
Your code doesn't compile.
You can't invoke a method from your "client08" class unless you create an instance of the class or make the method static.
So the code should be:
//String w=test();
client08 client = new client08();
String w= client.test();
Or you need to make the method static:
public static String test() {
Then you would invoke the method using:
String w = client08.test();
System.out.println(w);

String buffer not printing 'Character' [duplicate]

This question already has answers here:
Where did the 'M' go? [duplicate]
(4 answers)
Closed 5 years ago.
I checked the following code found that instead of printing A123 it is priting 123 .
Can some one explain what's going on here.
public class Test{
public static void main(String[] args) {
StringBuffer sb = null;
sb = new StringBuffer('A');
sb.append('1');
sb.append('2');
sb.append('3');
System.out.println(sb);//Printing 123
}
You're calling the constructor that specifys the capacity. Try this
sb = new StringBuffer("A");
You encountered int to char conversion.
You are invoking the constructor StringBuffer(int capacity).
public StringBuffer(int capacity) {
super(capacity);
}
Since you passing the char, it converted to int value (ASCII value) and taking as capacity.

Storing reference to object [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I would like to store reference to variable in some class, and make operations on it inside this class. Operations should modify original variable.
In particular following code should print 1 instead of 0.
class Test {
private Long metric;
public Test(Long m) {
this.metric = m;
++this.metric;
}
}
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Long metric = 0L;
Test test = new Test(metric);
System.out.println(metric);
}
}
How to achieve this behaviour?
You can replace Long with AtomicLong which is mutable. You'll lose autoboxing features though.
The problem in your code is that Integer is an immutable class.
Every time that you change the value you are really building a new instance of Integer.
Doing the same with mutable objects will work.
For example
class Test {
private StringBuilder metric;
public Test(StringBuilder m) {
this.metric = m;
this.metric.append(" Xter");
}
}
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
StringBuilder b = new StringBuilder("Hello ");
Test test = new Test(metric);
System.out.println(b.toString());
// Will print Hello Xter
}
}
As already metioned, the primitive wrapper classes are inmutable.
Since your metric is private in Test, and you want to use its value in the calling method main, you should use the java bean guidelines and use a getter for it:
public Long getMetric(){return this.metric;}
And on main:
metric=test.getMetric();
System.out.println(metric);

StringBuffer method parameter - doesn't change value [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I know that String is immutable and it's value can't be changed, but why does the value of the below StringBuffer doesn't change when sent like a method parameter. From my understanding it should have changed with the new value "bb". Thank you for your help.
class Ideone {
public static void main (String[] args) {
String s = "aa";
StringBuffer sb = new StringBuffer("aa");
modify(s, "bb");
modify2(sb, "bb");
System.out.println(s);
System.out.println(sb);
}
public static void modify(String s, String ss) {
s = ss;
}
public static void modify2(StringBuffer sb, String ss) {
sb = new StringBuffer(ss);
}
}
The universal rule in Java is that you cannot change the reference of an object passed into a method, but you can change its contents.
public static void modify2(StringBuffer sb, String ss){
This method takes a copy of a reference to a StringBuffer. Changing that reference to point to an object has no effect whatsoever on the original object. But if you implemented it as
sb.clear();
sb.append(ss);
then it would work.
Again, the rule is that reassigning an object passed into a method with = does nothing to the original object, but you can change the contents of that object just fine.

Categories