This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
How to override toString() properly in Java?
(15 answers)
Closed 2 years ago.
I'm trying to fetch a list from MySQL on android but I'm getting
[com.trycatchsoft.app.Models.KatPojo#7553fa]
as output instead of list.
Here is my pojo file:
package com.trycatchsoft.app.Models;
public class KatPojo {
private Boolean tf;
private String veri;
private String verid;
public Boolean getTf() {
return tf;
}
public void setTf(Boolean tf) {
this.tf = tf;
}
public String getVeri() {
return veri;
}
public void setVeri(String veri) {
this.veri = veri;
}
public String getVerid() {
return verid;
}
public void setVerid(String verid) {
this.verid = verid;
}
}
By default, printing an Object results in a class name and hash code. One can change this result by overriding the toString() method inherited by all children of Object.
When you're working with the Object type, you need to override the toString() method and call this method if you want to get the string representation of this object.
you can also use https://github.com/google/gson
If you are debugging I find it easy to just print the json instead of writing toString. You can pass your object or an array of objects.
Gson GSON = new GsonBuilder().setPrettyPrinting().create();
System.out.println(GSON.toJson(<PASS YOUR OBJECT REFERENCE HERE>));
Related
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 4 years ago.
am writing a program on linked list in Java but am confused on why the right values are not been added. I want to add the student name and StudentNo together as they for one student. Below is the code.
package linked;
public class PlsWork {
private String name;
private int studentNo;
public PlsWork(String name, int studentNo){
this.name=name;
this.studentNo=studentNo;
}
public String getname(){
return name;
}
public int getstudentNo(){
return studentNo;
}
}
package linked;
import java.util.LinkedList;
public class Linked {
public static void main(String[] args) {
LinkedList myLinkedList = new LinkedList();
myLinkedList.addFirst("A");
System.out.println(myLinkedList);
PlsWork ok = new PlsWork("obinna",3);
myLinkedList.add(ok);
System.out.println(myLinkedList);
}
}
when i run the code i get the answer below
[A, linked.PlsWork#6d06d69c]
Instead of [A,obinna 3]
linked.PlsWork#6d06d69c is the reference to your object. That is what toString shows by default. You may want to override toString on your PlsWork-class for example as follows:
class PlsWork {
...
#Override
public String toString() {
return name + ' ' + studentNo;
}
}
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 5 years ago.
I have an generic array class that stores other objects. When I try to get the object values from the generic array it prints the insert objects classpath. How can I access the value?
public class Shop implements Initializable {
#FXML private TextField name;
#FXML private TextField quantity;
#FXML private TextArea list;
MyArrayList<Item> array = new MyArrayList<>();
#Override
public void initialize(URL location, ResourceBundle resources) {
}
#FXML
private void addItem() {
int q = Integer.parseInt(quantity.getText());
String n = name.getText();
Item item = new Item(n,q);
MyArrayList.getInstance().add(item);
System.out.println(MyArrayList.getInstance().getItem(0));
//Outputs sample.Item#1674184b instead of the value Banana.
}
So your are putting an Integer and a String into the Item?
MyArrayList.getInstance().getItem(0).getName() or
MyArrayList.getInstance().getItem(0).getQuantity()
should print out the values u put it.
If you dont have the get methods create them, or if the fields are public you could access them directly.
U might want to overwrite the toString() method if u want to get all the values from the class listed like.
#Overwrite
public String toString(){
return name + "," + quantity;
}
And call
System.out.println(MyArrayList.getInstance().getItem(0).toString());
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am developing an App which uses the following code. It is generating an unexpected error as "Attempt to invoke virtual method on a null object reference". I do not understand the reason why this is happening. The error is thrown as the line containing t[i].setTestname(getData(exams[i]));. Could someone please point out what I am doing wrong. Could use some help over here.
void processTestPerformance()
{
String exams[],rawdata;
rawdata=data.toString();
int numberoftests=getNumbers("tabletitle03",rawdata);
Tests[] t= new Tests[numberoftests];
exams=new String[numberoftests];
rawdata=rawdata+"tabletitle03";
for(int i=0;i<numberoftests;i++)
{
int j=rawdata.indexOf("tabletitle03");
int k=(rawdata.substring(j+1)).indexOf("tabletitle03");
exams[i]=rawdata.substring(j,j+1+k);
t[i].setTestname(getData(exams[i]));
rawdata=rawdata.substring(k);
}
}
The code for class Tests is as follows:
public class Tests
{
public int numberofsubjects;
public String testname;
public Subject s[];
public void setS(Subject[] s)
{
this.s = s;
}
public void setNumberofsubjects(int numberofsubjects)
{
this.numberofsubjects = numberofsubjects;
s=new Subject[numberofsubjects];
}
public void setTestname(String testname)
{
this.testname = testname;
}
}
Thanks in Advance.
You create an empty array of Tests class, of size numberoftests
If you look inside that array you will find a sequence of null. Because you never initialize it.
You just need to populate the array so that t[i] will return an instance of your class.
In your for-cycle you can for example use the default constructor:
t[i] = new Tests();
// now you can manipulate the object inside the array
t[i].etTestname(getData(exams[i]));
for(int i=0;i<numberoftests;i++)
t[i]=new Tests();
That solved my problem.
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 6 years ago.
So in my code, I made a class named Pet, that would have both a default constructor and a non-default constructor that passes in String name, and int age of the pet.
public class Pet
{
// instance variables
private int age;
private String name;
/**
* Constructor for objects of class Pet
*/
public Pet()
{
// initialise instance variables
age = 0;
name = "somePet";
}
public Pet(int age, String name)
{
this.age = age;
this.name = name;
}
}
Then I created a class named petArray that would add to the array and print out the array...
public class PetArray
{
// instance variables
private Pet [] petArray;
/**
* Constructor for objects of class PetArray
*/
public PetArray()
{
// initialise instance variables
petArray = new Pet[5];
}
public void addPets()
{
// put your code here
Pet myPet = new Pet(4, "Spots");
petArray[0] = (myPet);
petArray[1] = new Pet(2, "Lucky");
petArray[2] = new Pet(7, "Joe");
}
public void printPets()
{
for (int i = 0; i < petArray.length; i++)
{
System.out.println(petArray[i]);
}
}
}
But then, I get this in the terminal window when trying to print it out...
Pet#13255e3c
Pet#171ac880
Pet#52185407
null
null
You forgot to override toString() method inherited from Object.
In this case you can use something like this:
#Override
public String toString() {
return (name + age);
}
Java compiler just does not know how to print it, you have to inform it :)
In your addPets() method you are only adding 3 objects to your petArray while there are 2 more spaces you need to fill as you declared that array to be a length of 5.
You could change the length of your array down to 3 or you could add 2 more objects, that should fix your problem.
And as stated above, adding the toString method will get rid of the addressing issues.
class Pet {
...
#Override
public String toString(){
// the string passed from here will be shown in console
}
}
Your output is fine according to your code. You have to override toString() method to print as per your requirement. Add this may be it will help.
#override
public string toString(){
return "your required string"; // i.e : name or name+age
}
This question already has answers here:
How to access ArrayList from another class in Android Java?
(3 answers)
Closed 7 years ago.
I'm new in Java/Android and I'm trying to do one thing, but I'm not sure if I can or I can't do it.
My problem is this: I'm parsing a Json and I send this json to my class. All is correct, json works and the data is stored correctly. That I want to do, is access to the data that I've stored in the arrayList from another class, but I don't know how to do it.
I've tried to implement a singleton java class, but I can't access to the data.
That I said is for exampl. If I create this method I can access to the data, but I need to pass the data from my json to the method.
public String showOverlay(ArrayList<ScreenEvent> config){
String show = "";
String empty = "empty";
for(ScreenEvent client : config){
show = client.action;
if(show.equals("show"))
return show;
}
return empty;
}
I don't want to do this. I want to be able to create an object of the arrayList inside of my method:
public String myMethod(){
//I want access here to the data of the arrayList
return empty;
}
I read a json and pass the data in a ArrayList:
public static ArrayList<VsClientConfig.ScreenEvent> eventConfig = new ArrayList<VsClientConfig.ScreenEvent>();
//JSON stuff
VsClientConfig.ScreenEvent vs = VsClientConfig.ScreenEvent.getScreenEvent(action, className, typeEvent, viewId, colourEvent);
eventConfig.add(vs);
This is my class:
public class VsClientConfig{
public String colour;
public String height;
public static class ScreenEvent {
public String action;
public String className;
public String typeEvent;
public String viewId;
public String colourEvent;
private static ScreenEvent miScreenEvent;
public static ScreenEvent getScreenEvent(String action, String className, String typeEvent, String viewId, String colourEvent) {
if (miScreenEvent == null) {
miScreenEvent = new ScreenEvent(action, className, typeEvent, viewId, colourEvent);
}
return miScreenEvent;
}
private ScreenEvent(String action, String className, String typeEvent, String viewId, String colourEvent) {
this.action = action;
this.className = className;
this.typeEvent = typeEvent;
this.viewId = viewId;
this.colourEvent = colourEvent;
}
}
public String myMethod(){
//I want access here to the data of the arrayList
return empty;
}
...
Create and initialize static arrayList in a Common class like below:
public class Common{
public static ArrayList<VsClientConfig.ScreenEvent> eventConfig=new ArrayList<>();
}
And assign if from wherever you want like:
//JSON stuff
VsClientConfig.ScreenEvent vs = VsClientConfig.ScreenEvent.getScreenEvent(action, className, typeEvent, viewId, colourEvent);
Common.eventConfig.add(vs);
Now Common.eventConfig (your arrayList) will be accessible to through your application