Get And Set return Null - java

My Get and Set return null.
I think it's missing something on my Set method.
My Authen.java :
public class Authen {
String sessionID;
public void setSessionID(String sessionID) {
this.sessionID = sessionID;
}
public String getSessionID(){
return this.sessionID;
}
}
My Set method :
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
My Get method returns null when I log sID
Authen at = new Authen();
String sID = at.getSessionID();

You're redeclaring at which is going to remove any values you set.
After you set the id, don't create a new Authen object
So change this:
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
Authen at = new Authen(); // This is where you create a new EMPTY authen
String sID = at.getSessionID();
to:
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
String sID = at.getSessionID();

You should use at like below...
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
String sID = at.getSessionID();
No need to renew at.

The reason why your sID is null is that you 'new' a brand new object as you declare 'at' object in the second time Authen at = new Authen();.
You do not need to initianite Authen again if you want to get the sID correctly.
Just do:
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
String sID = at.getSessionID();

You can transfer the Authen instance from class A to B if it is needed there (assuming class A creates the instance of B):
class A {
private void aMethod() {
String id="1234";
Authen at = new Authen();
at.setSessionID(id);
...
B b = new B(at);
}
}
class B {
private Authen authen;
public B(Authen at) {
this.authen = at;
...
}
private void anotherMethod() {
String sID = this.authen.getSessionID();
...
}
}
obviously this code is not complete, just to show the main idea - and just one of many possibilities.

Related

add array into array

I have data of tracks & tracklinks like folowing:
trackname - abc
links - www.abc.com
www.abc1.com
www.abc2.com
trackname - xyz
links - www.xyz.com
www.xyz1.com
www.xyz2.com
I want to make array with in array in Java. so final array would be:
trackdata = {
[0] {
[trackname] = 'abc',
[tracklinks] = {
[0] = "www.abc.com";
[1] = "www.abc1.com";
[2] = "www.abc2.com";
}
},
[1] {
[trackname] = 'xyz',
[tracklinks] = {
[0] = "www.xyz.com";
[1] = "www.xyz1.com";
[2] = "www.xyz2.com";
}
}
I have tried to make this using ArrayList, Map but not succeed.
Map<String,String> map = new HashMap<>();
map.put("trackname", "abc");
ArrayList<String> myLinks= new ArrayList<>();
myLinks.add("www.abc.com");
myLinks.add("www.abc1.com");
myLinks.add("www.abc2.com");
map.put("tracklinks", myLinks);
please help me here.
Consider using a multimap, a map whose values are list objects:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Starter {
public static void main(String[] args) {
Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
ArrayList<String> myLinks= new ArrayList<>();
myLinks.add("www.abc.com");
myLinks.add("www.abc1.com");
myLinks.add("www.abc2.com");
map.put("abc", myLinks);
System.out.println(map); // {abc=[www.abc.com, www.abc1.com, www.abc2.com]}
}
}
You should create a class and then access the properties like you want.
class TrackData {
private String trackme;
private List<String> trackLink;
public String getTrackme() {return trackme;}
public void setTrackme(String trackme) {this.trackme = trackme;}
public List<String> getTrackLink() {return trackLink;}
public void setTrackLink(List<String> trackLink) {this.trackLink = trackLink;}
}
To access it:
#Test
void arrayInArray_Test1() {
List<TrackData> trackData = new ArrayList<>();
trackData.add(new TrackData(){{
setTrackme("abc");
setTrackLink(new ArrayList<String>(){{
add("www.abc.com");
add("www.abc1.com");
add("www.abc2.com");
}});
}});
trackData.add(new TrackData(){{
setTrackme("xyz");
setTrackLink(new ArrayList<String>(){{
add("www.xyz.com");
add("www.xyz1.com");
add("www.xyz2.com");
}});
}});
System.out.println(trackData);
}
If you are using a newer Java version, you can create a record instead of a class.
You can achieve as follows
public class TrackTest {
public static void main(String[] args) {
List<Tracks> trackList = new ArrayList<>();
Tracks track1 = new Tracks("abc");
track1.getTrackLinks().add("www.abc.com");
track1.getTrackLinks().add("www.abc1.com");
track1.getTrackLinks().add("www.abc2.com");
Tracks track2 = new Tracks("xyz");
track2.getTrackLinks().add("www.xyz.com");
track2.getTrackLinks().add("www.xyz1.com");
track2.getTrackLinks().add("www.xyz2.com");
trackList.add(track1);
trackList.add(track2);
System.out.println(trackList);
}
static class Tracks{
private String trackName;
private List<String> trackLinks;
public Tracks(String trackName) {
this.trackName = trackName;
this.trackLinks = new ArrayList<>();
}
public Tracks(String trackName, List<String> trackLinks) {
this.trackName = trackName;
this.trackLinks = trackLinks;
}
public String getTrackName() {
return trackName;
}
public List<String> getTrackLinks() {
return trackLinks;
}
#Override
public String toString() {
return "Tracks [trackName=" + trackName + ", trackLinks=" + trackLinks + "]";
}
}
}
Let me know, if you want other approach.
how are u?
Why u dont do this.
Create class named URL, for example.
public class Url(){
//atributes
String domain;
String url;
//Contructor
public class URL(String domain, String url){
this.domain = domain;
this.url = url;
}
}
In ur main.class, u can create one Arraylist to saves ur instaces of URL.
public static void newURL(){
String domain, url;
Scanner keyboard = new Scanner(System.in);
//ask domain, i will use an example.
System.out.println("What is domain of URL?");
domain = keyboard.nextLine();
System.out.println("What is url?");
url = keyboard.nextLine;
//now, u have atributes of new url
URL url = new URL(domain,url);
}
What its ur objective? It's important question.
If is for simple control in a little program, u can do this

How can I get previous value java?

I have tow Spinner dropDown on my layout when user select each of spinner I'll take an id from spinner,and I have a class that I send these Ids to it class .
the getID.class :
public class getID {
private String tagID = "105358";
public getID tagID(String tagID) {
this.tagID += "," + tagID;
return this;
}
public URL build() throws MalformedURLException {
return new URL(
String.format("%s",
tagID));
}
}
Problem :
But when I select an item on second spinner I lost the first Value of the first spinner .
I send my value to class with this code:
URL url = new getID(Const.URLMedia)
.tagID("10")
.build();
For example when I select an item on first spinner(for exam I send 10 value) in other class I see :
105358,10
when I select an Item on second spinner(for exam I send 85 value) in other class I see :
105358,85
But I need to :
105358,10,85
Seems like you are creating a new instance of getID everytime:
URL url = new getID(Const.URLMedia)
.tagID("10")
.build();
so when you select the first spinner you get 105358,10 and when you select second, your code will again create new instance of getID and you get 105358,5 so simply create a single getID instance instead of creating a new one every time.
class Activity ..{
getID url;
#Override
oncreate (Bundle saveinstance){
url=new getID();
}
}
Now simple appends the value
URL url = obj.tagID(StringValue).build();
plus i can't see any constructor for this getID(Const.URLMedia),seems like missing.
Best Practices for some unexpected cases to avoid broken URL(if
sequence matter):
if user select 2nd spinner instead of first : you can create new getID object inside onclick of first spinner plus set the default value of 2nd spinner.
One way to do it is to keep track of the two sliders (I have added a constructor):
public class getID {
private String tagID;
private String init;
private String firstSlider;
private String secondSlider;
public getID setFirstSlider(String value) {
firstSlider = value;
return this;
}
public getID setSecondSlider(String value) {
secondSlider = value;
return this;
}
public getID(String init) {
this.init = init;
tagID = "" ;
firstSlider = "";
secondSlider = "";
}
public getID tagID() {
this.tagID = init + "," + firstSlider + "," + secondSlider;
return this;
}
public URL build() throws MalformedURLException {
return new URL(
String.format("%s",
tagID));
}
}
Then you can use the class like this:
try {
getID myID = new getID("105358") ;
URL url = myID.setFirstSlider("10").setSecondSlider("20").tagID().build();
System.out.println("url: " + url);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
There are other ways to do it, for example by removing the tagID() function and the tagID String and calling directly build() since all the info is available:
public URL build() throws MalformedURLException {
return new URL(
String.format("%s,%s,%s",
init, firstSlider, secondSlider));
}
}

Get a variable from an ArrayList that uses external class

I have an ArrayList called results that uses the class ItemObjects when I want to add an item in results. My problem is that I cannot manage to retrieve a specific String from an item.
The class code is:
public class ItemObjects {
private String mText1;
private String mText2;
public ItemObjects(String text1, String text2){
mText1 = text1;
mText2 = text2;
}
public String getmText1() {
return mText1;
}
public void setmText1(String mText1) {
this.mText1 = mText1;
}
public String getmText2() {
return mText2;
}
public void setmText2(String mText2) {
this.mText2 = mText2;
}
}
And I use the following code to add an item into the ArrayList:
ArrayList results = new ArrayList<ItemObjects>();
//THis part goes inside a for using i as increment;
ItemObjects obj = new ItemObjects(type, sender);
results.add(i , obj);
I have tried several things to retrieve the data such as:
String type = ItemObjects.getmText1();
or:
String type= results.get(i);
the first try, only retrieves the mText1 from the first item, and the second is an object and I dn't know how i should get the mText1 from it.
Any help would be appreciated :)
For adding the Value
ArrayList<ItemObjects> results = new ArrayList<ItemObjects>();
ItemObjects obj = new ItemObjects(type, sender);
results.add(obj);
For getting the Value
for (int i=0, i<result.size(); i++)
{
String type = results.get(i).mText1;
String sender= results.get(i).mText2;
Toast.makeText(this, "" + type, Toast.LENGTH_LONG).show();
Toast.makeText(this, "" + sender, Toast.LENGTH_LONG).show();
}
Change
ArrayList results = new ArrayList<ItemObjects>();
to
ArrayList<ItemObjects> results = new ArrayList<>();
Then results.get() will return ItemObjects.
Or you can simply cast your current result.get() to ItemObjects

Add ImageColumn

I should like to use an image in a column.
FastReportBuilder drb = new FastReportBuilder();
drb.addImageColumn("Example", expression, 20, true, ImageScaleMode.NO_RESIZE, myStyle);
CustomExpression iexpressionr = new CustomExpression() {
String ok = "http://....//ok.png";
String ko = "http://....//error.png";
public String getClassName() {
return String.class.getName();
}
public Object evaluate(Map fields, Map variables, Map parameters) {
String result = (String) fields.get("result");
if (result.equals("true")) {
return ok;
} else {
return ko;
}
}
};
My problem is the following: the style of the header is the default.
How Can I insert the header style in this case?
I try
ImageColumn d = new ImageColumn();
d.setExpression(imgExpr);
d.setTitle("Example");
d.setWidth(20);
d.setHeaderStyle(myHeaderStyle);
d.setStyle(myStyle);
but the method "addColumn" for the object FastReportBuilder it is not good.

List stores the last added value only

I have a problem with the following code:
First of all i have an inner class:
public class TraceMessage{
private String messageType;
private String tracedIdentifier;
private List<String> content;
TraceMessage(){
content = new ArrayList<String>();
messageType="";
tracedIdentifier="";
}
TraceMessage(String messageType, String identifier ,List<String> content){
this.messageType = messageType;
this.tracedIdentifier = identifier;
this.content = content;
}
It has getters and setter for the 3 attribute. My problem is:
I have a list of this messages:
private List<TraceMessage> messages = new ArrayList<TraceMessage>();
and I'm trying to add new elements to this like that:
messages.add(new TraceMessage(temp.messageType,temp.tracedIdentifier,temp.content));
where temp is an TraceMessage object.
So My problem when i add a message type object like that to the List the values are fine I even put printout to the Constructor and it has also shows the good value. But later When I'm trying to use that List all elements of the list has the same content(the last one). What could be the problem?
Here is the full part where i add the messages:
String fileName="tracefile.MTR";
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
String line;
TraceMessage temp = new TraceMessage();
while ((line=br.readLine()) != null) {
if(line.contains("MSCi")){
temp.content.clear();
temp.content.add(line);
}
else if(line.contains("CALL PHASE")){
temp.messageType = line.substring(60);
temp.content.add(line);
}
else if(line.contains("CALL ID")){
temp.tracedIdentifier = line.substring(22);
temp.content.add(line);
}
else if(line.contains("END OF REPORT")){
temp.content.add(line);
messages.add(new TraceMessage(temp.messageType,temp.tracedIdentifier,temp.content));
}
else{
temp.content.add(line);
}
}
} finally {
br.close();
}
I would rewrite the second constructor to take copy of list
TraceMessage(String messageType, String identifier ,List<String> content){
this.messageType = messageType;
this.tracedIdentifier = identifier;
this.content = new ArrayList<String>(content);
}
This will copy temp.content that you are reusing in while loop. Instances will be safe from operations on contents collection executed outside of the class.
put TraceMessage temp = new TraceMessage();
inside the while loop
and change messages.add(new TraceMessage(temp.messageType,temp.tracedIdentifier,temp.content));
to
messages.add(temp);
Ok if you can't do that
replace messages.add(new TraceMessage(temp.messageType,temp.tracedIdentifier,temp.content));
with
messages.add(temp); messages = new TraceMessage();
Change your code like this:
...
try {
String line;
List<String> content = new ArrayList<String>();
String messageType = "";
String tracedIdentifier = "";
while ((line=br.readLine()) != null) {
if (line.contains("MSCi")){
content.clear();
content.add(line);
}
else if (line.contains("CALL PHASE")) {
messageType = line.substring(60);
content.add(line);
}
else if (line.contains("CALL ID")) {
tracedIdentifier = line.substring(22);
content.add(line);
}
else if (line.contains("END OF REPORT")) {
content.add(line);
messages.add(new TraceMessage(messageType, tracedIdentifier, content);
}
else {
content.add(line);
}
}
} finally {
br.close();
}
Instead of using a temp object, save your attributes in external variables (out of the while loop) and then add a new object to your messages list with those variables.

Categories