Why when I print my array of objects it shows all null? - arrays

I want to create a deck of cards(object), and it shows nullnullnull... however i construct the card.
This is the class I use to initialize the deck:
import black.jack.bean.Card;
public class InitDeck {
public InitDeck() {
}
public void createDeck(Card[] cards) {
for(int idx = 0; idx<cards.length; idx++) {
int contValueNum = 1;
boolean valueYes = false;
cards[idx] = new Card((char) idx, idx);
if(!valueYes) {
switch(contValueNum) {
case 1:
cards[idx].setKey('A');
cards[idx].setKeyValue(contValueNum);
break;
case 11:
cards[idx].setKey('J');
cards[idx].setKeyValue(contValueNum);
break;
case 12:
cards[idx].setKey('Q');
cards[idx].setKeyValue(contValueNum);
break;
case 13:
cards[idx].setKey('K');
cards[idx].setKeyValue(contValueNum);
break;
}
if(idx==cards.length) {
valueYes = false;
}
}
}
}
public void visDeck(Card[] cards) {
for(int idx = 0; idx<cards.length; idx++) {
System.out.print(cards[idx]);
}
}
}
This is the class Deck and the class Card
public class Deck {
private static final int CARDS = 52;
//private char[] deck = new char[CARDS];
protected Card[] cards = new Card[CARDS];
public Deck() {
}
public Card[] getCards() {
return cards;
}
public void setCards(Card[] cards) {
this.cards = cards;
}
}
public class Card {
public char key;
public int keyValue;
public Card(char key, int keyValue) {
this.key = key;
this.keyValue = keyValue;
}
public Card() {
}
public char getKey() {
return key;
}
public void setKey(char key) {
this.key = key;
}
public int getKeyValue() {
return keyValue;
}
public void setKeyValue(int keyValue) {
this.keyValue = keyValue;
}
}
And this is my Console
public class Console {
public Console() {
}
public void execute() {
InitDeck initDeck= new InitDeck();
Deck deck = new Deck();
Card card = new Card();
initDeck.visDeck(deck.getCards());
}
}
And the result is:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
There's someone who could help me out?

Related

My unity save system doesn't save integer array

I don't get any errors but my script doesn't load or possibly save my array...
here's the first script
[System.Serializable]
public class PlayerData
{
public float health;
public float thirst;
public float hunger;
public float oxygen;
public float[] position;
public int[] inventoryIDs;
public PlayerData (Health healthO, SaveLoad saveload)
{
//Save int items
health = healthO.health;
thirst = healthO.thirst;
hunger = healthO.hunger;
oxygen = healthO.oxygen;
//set and save location array
position = new float[3];
position[0] = healthO.transform.position.x;
position[1] = healthO.transform.position.y;
position[2] = healthO.transform.position.z;
//set and save inventory IDs
inventoryIDs = new int[50];
for(int i = 0; i < 50; i++)
{
inventoryIDs[i] = saveload.IDs[i];
}
}
}
here's the next
public class SaveLoad : MonoBehaviour
{
public GameObject player;
public int[] IDs;
public GameObject[] objects;
Inventory inventory;
Health health;
void Start()
{
IDs = new int[50];
objects = new GameObject[50];
inventory = player.GetComponent<Inventory>();
health = player.GetComponent<Health>();
}
void Update()
{
//Add IDs
for (int i = 0; i < 50; i++)
{
IDs[i] = inventory.slot[i].GetComponent<Slot>().ID;
}
//debug save load test
if (Input.GetKeyDown(KeyCode.Z))
{
SaveP();
}
if (Input.GetKeyDown(KeyCode.X))
{
LoadP();
}
}
public void SaveP()
{
SaveSystem.SavePlayer(health, this);
}
public void LoadP()
{
PlayerData data = SaveSystem.LoadPlayer();
//load stats
health.thirst = data.thirst;
health.hunger = data.hunger;
health.health = data.health;
health.oxygen = data.oxygen;
//Load position
Vector3 position;
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
player.transform.position = position;
//load IDs
for (int i = 0; i < 50; i++)
{
IDs[i] = data.inventoryIDs[i];
}
//Load Items
for (int i = 0; i < 50; i++)
{
if(objects[IDs[i]] != null)
{
GameObject itemObject = (GameObject)Instantiate(objects[IDs[i]], new Vector3(0, 0, 0), Quaternion.identity);
Item item = itemObject.GetComponent<Item>();
inventory.AddItem(itemObject, item.ID, item.type, item.name, item.description, item.icon);
} else
{
return;
}
}
}
}
Here's the last script
public static class SaveSystem
{
public static string fileName = "FileSave.bin";
public static void SavePlayer(Health health, SaveLoad SL)
{
//Create formatter
BinaryFormatter bf = new BinaryFormatter();
// Create file stream
FileStream file = File.Create(GetFullPath());
//Save data
PlayerData data = new PlayerData(health, SL);
bf.Serialize(file, data);
//Close stream
file.Close();
}
public static PlayerData LoadPlayer()
{
if (SaveExists())
{
try
{
//Create formatter
BinaryFormatter bf = new BinaryFormatter();
//Create file stream
FileStream file = File.Open(GetFullPath(), FileMode.Open);
//Load data
PlayerData pd = (PlayerData)bf.Deserialize(file);
//close stream
file.Close();
//return data
return pd;
}
catch (SerializationException)
{
Debug.Log("Failed to load file at: " + GetFullPath());
}
}
return null;
}
private static bool SaveExists()
{
return File.Exists(GetFullPath());
}
private static string GetFullPath()
{
return Application.persistentDataPath + "/" + fileName;
}
}
there are all connected with the save load script loading and saving the variables into the player and items to the inventory sots. the inventory IDs array isn't saving or loading

Rest assured - Response body displays null value for Success

Response body displays null value for Success
POJO :
public class Employee {
public String name;
public int salary;
public int age;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "Employee[name=" + name + ",salary=" + salary + ",age=" + age + "]";
}
JSON Reader Class :
public class ReadJsonFile < T > {
public < T > List < T > getjsondata(String filepath, Class < T > typeofT) {
//filepath= "src/test/resource/testdataresource/addplacerequest.json";
Gson gson = new Gson();;
BufferedReader bufferReader = null;
try {
bufferReader = new BufferedReader(new FileReader(filepath));
//Type ListType = new TypeToken<ArrayList<Addbook>>(){}.getType();
Type ListType = TypeToken.getParameterized(ArrayList.class, typeofT).getType(); //
//gson.toJson(ReadJsonFile,ListType);
//return new Gson().fromJson(bufferReader, ListType);
List < T > arraylist = gson.fromJson(bufferReader, ListType);
return (arraylist);
} catch (FileNotFoundException e) {
throw new RuntimeException("Json file not found at path : " + filepath);
} finally {
try {
if (bufferReader != null) bufferReader.close();
} catch (IOException ignore) {}
}
}
}
Class to Read Pojo and Json Reader :
public class TestDataBuild {
public static List < Employee > employee() {
ReadJsonFile < Employee > readJson = new ReadJsonFile < Addbook > ();
List < Addbook > employee = readJson.getjsondata("src/test/resource/testDataResource/addplacerequest.json", Employee.class);
//Retrieve data from JsonObject and create Employee bean
for (int i = 0; i < employee.size(); i++) {
//System.out.println(addbook.get(i).name);
addbook.get(i).setName(employee.get(i).name);
addbook.get(i).setAge(employee.get(i).age);
addbook.get(i).setSalary(employee.get(i).salary);
}
return Employee;
}
}

downloadUrlToStorageInBackground in ImageList model for imageViewer downloads & overrides the image every time

class ImageList implements ListModel<Image> {
private int selection;
private Image[] images;
private EventDispatcher listeners = new EventDispatcher();
public ImageList() {
this.images = new EncodedImage[imageURLs.length];
}
public Image getItemAt(final int index) {
if (images[index] == null) {
images[index] = placeholderForTable;
Util.downloadUrlToStorageInBackground(imageURLs[index], "list" + index, (e) -> {
try {
images[index] = EncodedImage.create(Storage.getInstance().createInputStream("list" + index));
listeners.fireDataChangeEvent(index, DataChangedListener.CHANGED);
} catch (IOException err) {
err.printStackTrace();
}
});
}
return images[index];
}
public int getSize() {
return imageURLs.length;
}
public int getSelectedIndex() {
return selection;
}
public void setSelectedIndex(int index) {
selection = index;
}
public void addDataChangedListener(DataChangedListener l) {
listeners.addListener(l);
}
public void removeDataChangedListener(DataChangedListener l) {
listeners.removeListener(l);
}
public void addSelectionListener(SelectionListener l) {
}
public void removeSelectionListener(SelectionListener l) {
}
public void addItem(Image item) {
}
public void removeItem(int index) {
}
}
protected void postMenuForm(Form f) {
BusinessForumImagesConnection bfic = new BusinessForumImagesConnection();
bfic.businessForumImagesConnectionMethod(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
if (bfic.response != null) {
for (int i = 0; i < imgLoop; i++) {
HashMap hm = (HashMap) bfic.response.get(i);
String imgUrl = (String) hm.get("imgUrl");
imageURLs[i] = imgUrl;
}
}
}
});
if (imageURLs != null) {
ImageList imodel = new ImageList();
ImageViewer iv = new ImageViewer(imodel.getItemAt(0));
iv.setImageList(imodel);
Container adsContainer = BoxLayout.encloseY(adsLabel, iv);
slideIndex = 0;
Runnable r = new Runnable() {
public void run() {
if (slideIndex < imodel.getSize()) {
nextImage = (Image) imodel.getItemAt(slideIndex);
if (nextImage != null) {
iv.setImage(nextImage);
}
slideIndex++;
} else {
slideIndex = 0;
}
}
};
if (uITimer == null) {
uITimer = new UITimer(r);
}
if (uITimer != null) {
uITimer.schedule(5000, true, f); //5 seconds
}
f.add(BorderLayout.SOUTH, adsContainer);
adsContainer.setLeadComponent(adsLabel);
adsLabel.addActionListener((e) -> {
showForm("BusinessForum", null);
});
}
}
I had used URLImage.createToStorage before but imageViewer didnt work properly so I have used ImageList model. But everytime the form is opened, it jst redownloads the imgs and overrides them in storage, that makes the app slower. How can I make sure if the image is already downloaded, it doesnt download it again and jst shows them in imgViewer? thankyou
The download method will always download regardless...
You need to check if the Storage file exists and if so load that.
See the WebServices/Dogs demo in the new kitchen sink: http://www.codenameone.com/blog/kitchensink-ii.html

WPF : how to iterate through observable collection items and store specific items in a list of double type

I have an Observable collection containing some properties. My requirement is i want to iterate through observable collection properties and store/add properties from budgetReportYear1 to budgetReportYear30 in a separate list (double type).
Model:-
namespace RedCube45Mvvm.Model
{
public class RangeData : INotifyPropertyChanged
{
#region Constructor
public RangeData(MainWindowViewModel viewModel)
{
m_ViewModel = viewModel;
}
#endregion
#region Fields
private MainWindowViewModel m_ViewModel;
private string name;
private int fleet_size;
private double availability;
#endregion
#region Properties
public string Name
{
get { return name; }
set
{
name = value;
}
}
public double Availability
{
get { return availability; }
set
{
availability = value;
Onchanged("Availability");
var u = new Operations.Process.Updater(m_ViewModel, this, "avail");
}
}
public int FleetSize
{
get { return fleet_size; }
set
{
fleet_size = value;
Onchanged("FleetSize");
var u = new Operations.Process.Updater(m_ViewModel, this, "fleetSize");
}
}
#endregion
#region Budget Report Year
private double _budgetReportYear1 = 1;
private double _budgetReportYear2 = 1;
private double _budgetReportYear3 = 1;
private double _budgetReportYear4 = 1;
private double _budgetReportYear5 = 1;
private double _budgetReportYear6 = 1;
private double _budgetReportYear7 = 1;
private double _budgetReportYear8 = 1;
private double _budgetReportYear9 = 1;
private double _budgetReportYear10 = 1;
private double _budgetReportYear11 = 1;
private double _budgetReportYear12 = 1;
private double _budgetReportYear13 = 1;
private double _budgetReportYear14 = 1;
private double _budgetReportYear15 = 1;
private double _budgetReportYear16 = 1;
private double _budgetReportYear17 = 1;
private double _budgetReportYear18 = 1;
private double _budgetReportYear19 = 1;
private double _budgetReportYear20 = 1;
private double _budgetReportYear21 = 1;
private double _budgetReportYear22 = 1;
private double _budgetReportYear23 = 1;
private double _budgetReportYear24 = 1;
private double _budgetReportYear25 = 1;
private double _budgetReportYear26 = 1;
private double _budgetReportYear27 = 1;
private double _budgetReportYear28 = 1;
private double _budgetReportYear29 = 1;
private double _budgetReportYear30 = 1;
public bool Loaded { get; set; }
public double budgetReportYear1
{
get { return _budgetReportYear1; }
set
{
_budgetReportYear1 = value;
Onchanged("budgetReportYear1");
}
}
public double budgetReportYear2
{
get { return _budgetReportYear2; }
set
{
_budgetReportYear2 = value;
Onchanged("budgetReportYear2");
}
}
public double budgetReportYear3
{
get { return _budgetReportYear3; }
set
{
_budgetReportYear3 = value;
Onchanged("budgetReportYear3");
}
}
public double budgetReportYear4
{
get { return _budgetReportYear4; }
set
{
_budgetReportYear4 = value;
Onchanged("budgetReportYear4");
}
}
public double budgetReportYear5
{
get { return _budgetReportYear5; }
set
{
_budgetReportYear5 = value;
Onchanged("budgetReportYear5");
}
}
public double budgetReportYear6
{
get { return _budgetReportYear6; }
set
{
_budgetReportYear6 = value;
Onchanged("budgetReportYear6");
}
}
public double budgetReportYear7
{
get { return _budgetReportYear7; }
set
{
_budgetReportYear7 = value;
Onchanged("budgetReportYear7");
}
}
public double budgetReportYear8
{
get { return _budgetReportYear8; }
set
{
_budgetReportYear8 = value;
Onchanged("budgetReportYear8");
}
}
public double budgetReportYear9
{
get { return _budgetReportYear9; }
set
{
_budgetReportYear9 = value;
Onchanged("budgetReportYear9");
}
}
public double budgetReportYear10
{
get { return _budgetReportYear10; }
set
{
if (value < 0.01 || value > 99)
{
throw new ArgumentException("Value must be within 0.01 & 99");
}
_budgetReportYear10 = value;
Onchanged("budgetReportYear10");
}
}
public double budgetReportYear11
{
get { return _budgetReportYear11; }
set
{
_budgetReportYear11 = value;
Onchanged("budgetReportYear11");
}
}
public double budgetReportYear12
{
get { return _budgetReportYear12; }
set
{
_budgetReportYear12 = value;
Onchanged("budgetReportYear12");
}
}
public double budgetReportYear13
{
get { return _budgetReportYear13; }
set
{
_budgetReportYear13 = value;
Onchanged("budgetReportYear13");
}
}
public double budgetReportYear14
{
get { return _budgetReportYear14; }
set
{
_budgetReportYear14 = value;
Onchanged("budgetReportYear14");
}
}
public double budgetReportYear15
{
get { return _budgetReportYear15; }
set
{
_budgetReportYear15 = value;
Onchanged("budgetReportYear15");
}
}
public double budgetReportYear16
{
get { return _budgetReportYear16; }
set
{
_budgetReportYear16 = value;
Onchanged("budgetReportYear16");
}
}
public double budgetReportYear17
{
get { return _budgetReportYear17; }
set
{
_budgetReportYear17 = value;
Onchanged("budgetReportYear17");
}
}
public double budgetReportYear18
{
get { return _budgetReportYear18; }
set
{
_budgetReportYear18 = value;
Onchanged("budgetReportYear18");
}
}
public double budgetReportYear19
{
get { return _budgetReportYear19; }
set
{
_budgetReportYear19 = value;
Onchanged("budgetReportYear19");
}
}
public double budgetReportYear20
{
get { return _budgetReportYear20; }
set
{
_budgetReportYear20 = value;
Onchanged("budgetReportYear20");
}
}
public double budgetReportYear21
{
get { return _budgetReportYear21; }
set
{
_budgetReportYear21 = value;
Onchanged("budgetReportYear21");
}
}
public double budgetReportYear22
{
get { return _budgetReportYear22; }
set
{
_budgetReportYear22 = value;
Onchanged("budgetReportYear22");
}
}
public double budgetReportYear23
{
get { return _budgetReportYear23; }
set
{
_budgetReportYear23 = value;
Onchanged("budgetReportYear23");
}
}
public double budgetReportYear24
{
get { return _budgetReportYear24; }
set
{
_budgetReportYear24 = value;
Onchanged("budgetReportYear24");
}
}
public double budgetReportYear25
{
get { return _budgetReportYear25; }
set
{
_budgetReportYear25 = value;
Onchanged("budgetReportYear25");
}
}
public double budgetReportYear26
{
get { return _budgetReportYear26; }
set
{
_budgetReportYear26 = value;
Onchanged("budgetReportYear26");
}
}
public double budgetReportYear27
{
get { return _budgetReportYear27; }
set
{
_budgetReportYear27 = value;
Onchanged("budgetReportYear27");
}
}
public double budgetReportYear28
{
get { return _budgetReportYear28; }
set
{
_budgetReportYear28 = value;
Onchanged("budgetReportYear28");
}
}
public double budgetReportYear29
{
get { return _budgetReportYear29; }
set
{
_budgetReportYear29 = value;
Onchanged("budgetReportYear29");
}
}
public double budgetReportYear30
{
get { return _budgetReportYear30; }
set
{
_budgetReportYear30 = value;
Onchanged("budgetReportYear30");
}
}
public string Error
{
get { throw new NotImplementedException(); }
}
#endregion
#region Event Handler
public event PropertyChangedEventHandler PropertyChanged;
public void Onchanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
#endregion
}
}
View Model:-
public ObservableCollection<Model.RangeData> RangeData
{
get { return range_data; }
set {
range_data = value;
base.RaisePropertyChangedEvent("RangeData");
}
}

LZH in Byte Array Decompress in .NET?

An LZH archive is embedded within a file. The file was read into a byte[], and the LZH part is identified as a smaller byte[].
How can the embedded LZH bytes be decompressed into another byte[] using .NET Framework 4.6 (C#)? I have only see http://www.infoq.com/news/2008/06/7-Zip-from-.NET which doesn't exactly do what I need.
Thanks.
the code snippet that follows is taken from the sample program from this article
http://www.codeproject.com/Articles/27148/C-NET-Interface-for-Zip-Archive-DLLs
There are no significant changes: instead of reading and writing files, it reads and write byte arrays. Changes are marked by comments
Run with sevenzip.exe e "C:\temp\gwo0.11-sample-win32.lzh" 3 for example
https://dl.dropboxusercontent.com/u/71459360/7z.zip
using System;
using System.Collections.Generic;
using System.Text;
using Nomad.Archive.SevenZip;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
namespace SevenZip
{
class Program
{
private static void ShowHelp()
{
Console.WriteLine("SevenZip");
Console.WriteLine("SevenZip l {ArchiveName}");
Console.WriteLine("SevenZip e {ArchiveName} {FileNumber}");
}
static void Main(string[] args)
{
if (args.Length < 2)
{
ShowHelp();
return;
}
try
{
string ArchiveName;
uint FileNumber = 0xFFFFFFFF;
bool Extract;
switch (args[0])
{
case "l":
ArchiveName = args[1];
Extract = false;
break;
case "e":
ArchiveName = args[1];
Extract = true;
if ((args.Length < 3) || !uint.TryParse(args[2], out FileNumber))
{
ShowHelp();
return;
}
break;
default:
ShowHelp();
return;
}
using (SevenZipFormat Format = new SevenZipFormat(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "7z.dll")))
{
IInArchive Archive = Format.CreateInArchive(SevenZipFormat.GetClassIdFromKnownFormat(KnownSevenZipFormat.Lzh));
if (Archive == null)
{
ShowHelp();
return;
}
try
{
//the byte array is provided by you. here it's coming from a file
byte[] data;
using (var stream = File.OpenRead(ArchiveName))
{
data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
}
using (InStreamWrapper ArchiveStream = new InStreamWrapper(new MemoryStream(data))) //modified here
{
ulong CheckPos = 32 * 1024;
if (Archive.Open(ArchiveStream, ref CheckPos, null) != 0)
ShowHelp();
Console.Write("Archive: ");
Console.WriteLine(ArchiveName);
if (Extract)
{
PropVariant Name = new PropVariant();
Archive.GetProperty(FileNumber, ItemPropId.kpidPath, ref Name);
string FileName = (string) Name.GetObject();
Console.Write("Extracting: ");
Console.Write(FileName);
Console.Write(' ');
MemoryStream ms = new MemoryStream();
Archive.Extract(new uint[] { FileNumber }, 1, 0, new ArchiveMemoryCallback(FileNumber, ms)); //modified here
byte[] output = ms.ToArray(); //here you have the output byte array
output.ToString();
}
else
{
Console.WriteLine("List:");
uint Count = Archive.GetNumberOfItems();
for (uint I = 0; I < Count; I++)
{
PropVariant Name = new PropVariant();
Archive.GetProperty(I, ItemPropId.kpidPath, ref Name);
Console.Write(I);
Console.Write(' ');
Console.WriteLine(Name.GetObject());
}
}
}
}
finally
{
Marshal.ReleaseComObject(Archive);
}
}
}
catch (Exception e)
{
Console.Write("Error: ");
Console.WriteLine(e.Message);
}
}
}
class ArchiveCallback : IArchiveExtractCallback
{
private uint FileNumber;
private string FileName;
private OutStreamWrapper FileStream;
public ArchiveCallback(uint fileNumber, string fileName)
{
this.FileNumber = fileNumber;
this.FileName = fileName;
}
#region IArchiveExtractCallback Members
public void SetTotal(ulong total)
{
}
public void SetCompleted(ref ulong completeValue)
{
}
public int GetStream(uint index, out ISequentialOutStream outStream, AskMode askExtractMode)
{
if ((index == FileNumber) && (askExtractMode == AskMode.kExtract))
{
string FileDir = Path.GetDirectoryName(FileName);
if (!string.IsNullOrEmpty(FileDir))
Directory.CreateDirectory(FileDir);
FileStream = new OutStreamWrapper(File.Create(FileName));
outStream = FileStream;
}
else
outStream = null;
return 0;
}
public void PrepareOperation(AskMode askExtractMode)
{
}
public void SetOperationResult(OperationResult resultEOperationResult)
{
FileStream.Dispose();
Console.WriteLine(resultEOperationResult);
}
#endregion
}
//new
class ArchiveMemoryCallback : IArchiveExtractCallback
{
private uint FileNumber;
private Stream stream;
private OutStreamWrapper FileStream;
public ArchiveMemoryCallback(uint fileNumber, Stream stream)
{
this.FileNumber = fileNumber;
this.stream = stream;
}
#region IArchiveExtractCallback Members
public void SetTotal(ulong total)
{
}
public void SetCompleted(ref ulong completeValue)
{
}
public int GetStream(uint index, out ISequentialOutStream outStream, AskMode askExtractMode)
{
if ((index == FileNumber) && (askExtractMode == AskMode.kExtract))
{
FileStream = new OutStreamWrapper(stream);
outStream = FileStream;
}
else
outStream = null;
return 0;
}
public void PrepareOperation(AskMode askExtractMode)
{
}
public void SetOperationResult(OperationResult resultEOperationResult)
{
FileStream.Dispose();
Console.WriteLine(resultEOperationResult);
}
#endregion
}
}

Resources