Null pointer exception while trying to use DriverPropertyInfo[] - jdbc

Code:
DriverPropertyInfo[] Information = new DriverPropertyInfo[1];
String[] Names_Arr = new String[n.size()];
Names_Arr = n.toArray(Names_Arr);
Information[0].choices = Names_Arr;
I get a "Null pointer exception" in the last line. Why?
Note: n is an ArrayList of strings.

It's not really clear what you're actually trying to accomplish, but here is a very simple example of how to use the getPropertyInfo method of the JDBC Driver object:
String connectionUrl = "jdbc:hsqldb:mem:memdb";
Properties p = new Properties();
Driver d = DriverManager.getDriver(connectionUrl);
DriverPropertyInfo[] dpi = d.getPropertyInfo(connectionUrl, p);
for (int i = 0; i < dpi.length; i++) {
System.out.println(dpi[i].name);
}
The console output displays the name of each property in the array of DriverPropertyInfo objects that getPropertyInfo returned:
user
password
get_column_name
ifexists
default_schema
shutdown
More information on the attributes returned can be found at
Class DriverPropertyInfo

Related

File data not being read processing

I am absolutely stuck, my file data is not being read in processing using loadStrings();. I have tried everything. Here is my code:
Main Class
Student[] studentList;
String[] fileData;
void loadFileData(){
fileData = loadStrings("StudentData.txt");
}
void setup(){
loadFileData();
studentList = new Student[fileData.length/3];
for(int i = 0; i < fileData.length/3; i++){
studentList[i].studentName = fileData[i];
studentList[i].studentAddress = fileData[i+1];
studentList[i].studentNumber = parseInt(fileData[i+3]);
}
printArray(fileData);
}
Student Class
class Student{
String studentName;
String studentAddress;
int studentNumber;
Student(String name, String address, int number){
studentName = name;
studentAddress = address;
studentNumber = number;
}
}
My file directory is exactly as follows:
>Processing
>data
>StudentData.txt
>student
>studentSearch
All of the data is formatted in lines, name, address, number, repeat
Thank you!
I copied your code and ran it here in Processing.
I get a NullPointerException on this line:
studentList[i].studentName = fileData[i];
But it's not being caused by the fileData array. You can see this if you print the array immediately after it's loaded. The data is there:
fileData = loadStrings("StudentData.txt");
printArray(fileData);
You get an error because you're trying to access a Student object that doesn't exist. You create an array to hold the objects, but you don't actually put any objects in it before trying to access them in your loop. So studentList[i] is null.
You can pass the data directly to your Student constructor when you create the object:
studentList[i] = new Student(fileData[i], fileData[i+1], parseInt(fileData[i+3]));

Getting path of ArrayList<image> that is already set and convert to string

My program creates a "Deck" of cards in a List, I want to be able to get the image, find the path to parse it to get the information that is in the image name.
For this, I am using an ArrayList to store the cards.
List<Image> mainDeck = new ArrayList<Image>();
To load the image, I am using this code
public List load(List<Image> newDeck) {
count = 0;
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 10; k++) {
newDeck.add(new Image("images/" + prefix.get(i) + "" + (k + 1) + ".png"));
count++;
}// end number card for loop
for (int l = 0; l < 3; l++) {
newDeck.add(new Image("images/" + prefix.get(l) + "" + prefixFace.get(l) + ".png"));
count++;
}// end face card for loop
}// end deck for loop
it then gets called and populated with images that work perfectly, I would like to create a matching array filled with Strings that hold the path for the matching Image array.
The names of images are "c1.png", "c2.png", etc, and I just need the number in the pathname
Once I get the array I should be able to parse the data to get the numbers.
Any help would be much appreciated.
when using get url, I am getting an error, here is that code
for (Image card : mainDeck){
String path = card.getUrl();
String name = path.substring(path.lastIndexOf("/")+1, path.lastIndexOf("."));
nameData.put(card, name);
it is not recognizing card.getUrl();
you don't have to create another arrayList for the paths cause the path data is already saved with the images.
if you want to retrieve the path of an image after creating it then you might use the method getUrl() in the Image class, calling getUrl() from an Image object will return the path that you used to create the image when you called the constructor, note that it will only work if you called the Image constructor using a String as the path for the image, it will not work if you used an inputStream to initialize your image, and also that it was defined in java 9, after getting the path you might split it to get the useful data you want, something like
Image image = new Image("file:/C:/Users/zinou/Desktop/edit.png");
String path = image.getUrl();
String name = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
System.out.println(name);
and if i were to associate every card with its path, i would use a hashMap as in
List<Image> mainDeck = new ArrayList<Image>();
//population of the list
HashMap<Image, String> nameData = new HashMap<Image, String>();
for (Image card : mainDeck) {
String path = card.getUrl();
String name = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
nameData.put(card, name);
}
If you have java 8
you can create a class that extends the Image class and give it an additional attribute (the URL), and add a getter for the URL so you can access it, but then your cards will be instances of the new class you created, so you can get the URL for them, the new class may look like this
public class MyImage extends javafx.scene.image.Image{
String url;
public MyImage(String arg0) {
super(arg0);
url = arg0;
}
public String geturl() {
return url;
}
}

get workflow malfunction exception with java api

Does anyone know how to get a workflow malfunction error message using the java pe api? I am running the QueueSample java code provided by IBM and it is not clear to me how to do this. Any help would be appreciated!
I found the malfunction error message for my workflow in the VWParticipantHistory.getLogFields() array. I modified the example code from the Developing Applications with IBM FileNet P8 APIs redbook:
// Create session object and log onto Process Engine
...
// Get the specific work item
...
// Get VWProcess object from work object
VWProcess process = stepElement.fetchProcess();
// Get workflow definitions from the VWProcess
VWWorkflowDefinition workflowDefinition =
process.fetchWorkflowDefinition(false);
// Get maps for each workflow definition
VWMapDefinition[] workflowMaps = workflowDefinition.getMaps();
// Iterate through each map in the workflow Definition
for (int i = 0; i < workflowMaps.length; i++) {
// Get map ID and map name for each map definition
int mapID = workflowMaps[i].getMapId();
String mapName = workflowMaps[i].getName();
// Get workflow history information for each map
VWWorkflowHistory workflowHistory = process.fetchWorkflowHistory(mapID);
String workflowOriginator = workflowHistory.getOriginator();
// Iterate through each item in the Workflow History
while (workflowHistory.hasNext()) {
// Get step history objects for each workflow history
VWStepHistory stepHistory = workflowHistory.next();
String stepName = stepHistory.getStepName();
System.out.println("step history name = " + stepName);
// Iterate through each item in the Step History
while (stepHistory.hasNext()) {
// Get step occurrence history
// objects for each step history object
VWStepOccurrenceHistory stepOccurenceHistory = stepHistory.next();
Date stepOcurrenceDateReceived = stepOccurenceHistory.getDateReceived();
Date stepOcurrenceDateCompleted = stepOccurenceHistory.getCompletionDate();
while (stepOccurenceHistory.hasNext()) {
// Get step work object information
// for each step occurrence
VWStepWorkObjectHistory stepWorkObjectHistory = stepOccurenceHistory.next();
stepWorkObjectHistory.resetFetch();
// Get participant information for each work object
while (stepWorkObjectHistory.hasNext()) {
VWParticipantHistory participantHistory = stepWorkObjectHistory.next();
String opName = participantHistory.getOperationName();
System.out.println("operation name = " + opName);
Date participantDateReceived = participantHistory.getDateReceived();
String participantComments = participantHistory.getComments();
String participantUser = participantHistory.getUserName();
String participantName = participantHistory.getParticipantName();
VWDataField[] logFields = participantHistory.getLogFields();
System.out.println("** start get log fields **");
for (int index=0; index<logFields.length; index++){
VWDataField dataField = logFields[index];
String name = dataField.getName();
String val = dataField.getStringValue();
System.out.println("name = " + name + " , value = " + val);
}
System.out.println("** end get log fields **");
} // while stepWorkObjectHistory
} // while stepOccurenceHistory
} // while stepHistory
} // while workflowHistory
} // for workflow maps

Convert an object into list or array MVC

I have an MVC grid control that is passing back the collection to my controller as an object.
The object is an ArrayList of the rows from the grid and each row is a List that represents an instance of a class called lineitem that is part of a parent record class.
I am trying to load the object into the class by looping through the object array, create an instance of my lineitem class, and then add it to my parent record class parent.AddItem(lineitem). I originally had it created as follows
var items = requestData.ExtraRequestData["Items"];
However As I determined, it was an ArrayList I changed that to
List<string> mylist;
ArrayList items = (ArrayList)requestData.ExtraRequestData["Items"];
mylist = items.Cast<string>().ToList();
I am trying to convert the ArrayList into a List so I can loop through it and load it into my class.
Model.Parent parent = new Model.Parent();
Model.LineItem lineitem = new Model.LineItem();
for (int I = 0; I < mylist.length; I++){
lineitem.a = item.a,
lineitem.b = item.b
parent.AddItem(lineitem)
}
I am getting the following error on the line where I set mylist = items:
{"Unable to cast object of type 'System.Collections.Hashtable' to type 'System.String'."}
What am I missing to convert this so I can load it into my class?
Since each item in the ArrayList was a hashtable, I ended up looping through it as follows:
ParentClass ReconHdr = new ParentClass();
foreach (System.Collections.Hashtable o in items)
{
//lineItem.LineSeq = Convert.ToInt32(item.Value);
ArrayList list = new ArrayList(o.Values);
ChildClass lineItem = new ChildClass();
lineItem.StmntDocNum = list[0].ToString();
lineItem.PrtlAmt = Convert.ToDouble(list[1]);
lineItem.GLAcctCode = list[2].ToString();
lineItem.LineSeq = Convert.ToInt32(list[3]);
lineItem.ClinicID = Convert.ToInt32(list[4]);
lineItem.PrtlTranDate = Convert.ToDateTime(list[5]);
lineItem.StmntTranDate = Convert.ToDateTime(list[6]);
lineItem.PrtlFinRptDate = Convert.ToDateTime(list[7]);
lineItem.StmntEndDate = Convert.ToDateTime(list[8]);
lineItem.StmntAmt = Convert.ToDouble(list[9]);
lineItem.PrtlDocNum = list[10].ToString();
lineItem.PrtlUniqueID = list[11].ToString();
ReconHdr.AddItem(lineItem);
}

Dynamically choose which properties to get using Linq

I have an MVC application with a dynamic table on one of the pages, which the users defines how many columns the table has, the columns order and where to get the data from for each field.
I have written some very bad code in order to keep it dynamic and now I would like it to be more efficient.
My problem is that I don't know how to define the columns I should get back into my IEnumerable on runtime. My main issue is that I don't know how many columns I might have.
I have a reference to a class which gets the field's text. I also have a dictionary of each field's order with the exact property It should get the data from.
My code should look something like that:
var docsRes3 = from d in docs
select new[]
{
for (int i=0; i<numOfCols; i++)
{
gen.getFieldText(d, res.FieldSourceDic[i]);
}
};
where:
docs = List from which I would like to get only specific fields
res.FieldSourceDic = Dictionary in which the key is the order of the column and the value is the property
gen.getFieldText = The function which gets the entity and the property and returns the value
Obviously, it doesn't work.
I also tried
StringBuilder fieldsSB = new StringBuilder();
for (int i = 0; i < numOfCols; i++)
{
string field = "d." + res.FieldSourceDic[i] + ".ToString()";
if (!string.IsNullOrEmpty(fieldsSB.ToString()))
{
fieldsSB.Append(",");
}
fieldsSB.Append(field);
}
var docsRes2 = from d in docs
select new[] { fieldsSB.ToString() };
It also didn't work.
The only thing that worked for me so far was:
List<string[]> docsRes = new List<string[]>();
foreach (NewOriginDocumentManagment d in docs)
{
string[] row = new string[numOfCols];
for (int i = 0; i < numOfCols; i++)
{
row[i] = gen.getFieldText(d, res.FieldSourceDic[i]);
}
docsRes.Add(row);
}
Any idea how can I pass the linq the list of fields and it'll cut the needed data out of it efficiently?
Thanks, Hoe I was clear about what I need....
Try following:
var docsRes3 = from d in docs
select (
from k in res.FieldSourceDic.Keys.Take(numOfCols)
select gen.getFieldText(d, res.FieldSourceDic[k]));
I got my answer with some help from the following link:
http://www.codeproject.com/Questions/141367/Dynamic-Columns-from-List-using-LINQ
First I created a string array of all properties:
//Creats a string of all properties as defined in the XML
//Columns order must be started at 0. No skips are allowed
StringBuilder fieldsSB = new StringBuilder();
for (int i = 0; i < numOfCols; i++)
{
string field = res.FieldSourceDic[i];
if (!string.IsNullOrEmpty(fieldsSB.ToString()))
{
fieldsSB.Append(",");
}
fieldsSB.Append(field);
}
var cols = fieldsSB.ToString().Split(',');
//Gets the data for each row dynamically
var docsRes = docs.Select(d => GetProps(d, cols));
than I created the GetProps function, which is using my own function as described in the question:
private static dynamic GetProps(object d, IEnumerable<string> props)
{
if (d == null)
{
return null;
}
DynamicGridGenerator gen = new DynamicGridGenerator();
List<string> res = new List<string>();
foreach (var p in props)
{
res.Add(gen.getFieldText(d, p));
}
return res;
}

Resources