Can someone tell me why this is failing at
row.add(rs.getString(i))
rs=pst.executeQuery();
while(rs.next())
{
ObservableList<XYChart.Series<Date, Number>> row = FXCollections.observableArrayList();
for(int i =1; i<=rs.getMetaData().getColumnCount(); i++)
{
row.add(rs.getString(i));
}
}
The error i get is
no suitable method found for add(String)
method Collection.add(Series) is not applicable
(argument mismatch; String cannot be converted to Series)
method List.add(Series) is not applicable
(argument mismatch; String cannot be converted to Series)
I'm not sure what I should change.
Related
I am a beginner in Apex and I need your help. What I am trying to do is creating a class that returns an array of formatted strings. The class has as a parameter the number of strings and it returns the array of strings formatted as:
Test 0, Test 1, ...Test n
The error I get is:
unexpected token 'public (line 1).
There might be more than one error in my code, if yes please feel free to let me know.
Thank you in advance!
public class StringArrayTest {
public static void generateStringArray(Integer n){
//List<String> stringArray = new List<String>();
for(Integer i=0; i<n; i++){
List<String>stringArray = new List<String>{'Test '+i};
}
return stringArray[];
}
}
There are a few things wrong with the code you have posted. When I tried this out I didn't get the error you mentioned, but here are the things to fix and get your code working:
The method should declare a return value of List<String>
Uncomment the line to initialize your list before the loop.
Return just the variable itself, no need to have the brackets. It's already a list.
Here is working code:
public class StringArrayTest {
public static List<String> generateStringArray(Integer n){
List<String> stringArray = new List<String>();
for(Integer i = 0; i < n; i++){
stringArray.add('Test ' + i);
}
return stringArray;
}
}
My purpose is to get what a user types in and to store them in the array "info", then convert the info[0] into upper case. However, when I compile my code, I always got the message Exception in thread "main" java.lang.NullPointerException at the line "info[0]=info[0].toUpperCase();". But I totally have not idea what causes this exception. If anyone can tell me the cause, it would be great. Thank you!
public static void main(String[] args)
{
Scanner userScan = new Scanner(System.in);
String keyboard = userScan.nextLine();
StringTokenizer tokens = new StringTokenizer(keyboard, " ");
String[] info= new String[4];
for(int i=0; tokens.hasMoreTokens(); i++)
{
info[i] = tokens.nextToken();
}
info[0]=info[0].toUpperCase();
//other codes...
}
When you try to execute the code without any tokens, It would skip past the for loop, and try to perform
info[0].toUpperCase();
But since the for loop has been skipped info object is initialized to null.
Thus trying to access it would give you an Null Pointer Exception.
Just move the conversion inside the for loop to avoid this.
for(int i=0; tokens.hasMoreTokens(); i++){
info[i] = tokens.nextToken();
//converts only when value exists
info[i]=info[i].toUpperCase();
}
If I pass data via a Seque to another View Controller, e.g.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"mySequeIdentifier"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipientViewController *destViewController = segue.destinationViewController;
destViewController.intPassedRow = indexPath.row;
}
}
THis passes the correct row number but...
I get an error saying "Incompatible integer to pointer conversion assigning to 'int' from 'int'."
If they are both Ints then what have I done wrong?
I have an error when trying to run this program. This is not the entire program just the few details I think where the problem is. The error points out
if(board[randSpace].isEmpty()) and
cl.makeChutes(5);"
Error is :
"Exception in thread "main" java.lang.NullPointerException
at ChutesAndLadders.makeChutes(ChutesAndLadders.java:29)
at ChutesAndLadders.main(ChutesAndLadders.java:67)"
This is my program:
import java.util.Random;
public class ChutesAndLadders{
Cell [] board = new Cell [100];
Random rand = new Random ();
public int chutes, ladders;
public void makeChutes (int a ){
int makeChutes = a;
for (int i = 0; i < a; i++){
int randSpace = rand.nextInt(99);
if(board[randSpace].isEmpty())
board[randSpace] = new Cell (-10,"C");
else
i--;
}
}
public static void main(String[] args) {
// Create an instance of ChutesAndLadders
ChutesAndLadders cl = new ChutesAndLadders(10,10);
// Randomly place 5 more chutes and 5 more ladders
cl.makeChutes(5);
cl.makeLadders(5);
Here is my isEmpty:
public boolean isEmpty(){
for(int i = 0; i < board.length; i++)
{
if (board[i].isEmpty())
return true;
}
return false;
}
I could be entirely wrong on my coding. I'm still learning so please be patient. Thank you!
Following on what Dewfy said, board is an array of Cell objects. That means it is a placeholder for Cell objects --a bunch of nulls waiting to be replaced by a Cell.
If say, the object at position 1 (board[1]) has not been assigned a Cell object (i.e. you haven't issued a command like board[1]=new Cell() or something like that) then board[1] is null. Therefore if you were to ask board[1].isEmpty() you would get a NullPointerException because you are trying to call a method on a null object.
Now, your error is likely to come from board[randSpace] being null. So, the question is: does it contain a Cell object? Have you initialized your array with Cell objects?
One way to check this would be to say:
if (board[randSpace]==null || board[randSpace].isEmpty()) .....
Problem appears with this line:
if(board[randSpace].isEmpty())
so it is supposed that obect at randSpace already exists, if not you create object (Cell). But object is not yet created, how you can ask it if null isEmpty(). Just check against null first
I have a mapper method like this :
#InsertProvider(class=com.something.class, method="doSomething")
public void insertSomething(Set<Integer> set, int guideId);
and in the something class, I have a method :
public String doSomething(Set<Integer> set, int guideId){
// do something and returna a query
}
It gives me an error :
Error creating SqlSource for SqlProvider. Method 'doSomething' not
found in SqlProvider 'com.something.class'
When I debugged the issue.. I found that in the constructor of ProviderSqlResource, it throws this exception if the no. of arguments are 2 or more. I can't think of any reason why they would do that. What's the workaround ?
Here is the method :
public ProviderSqlSource(Configuration config, Object provider) {
String providerMethodName = null;
try {
this.sqlSourceParser = new SqlSourceBuilder(config);
this.providerType = (Class<?>) provider.getClass().getMethod("type").invoke(provider);
providerMethodName = (String) provider.getClass().getMethod("method").invoke(provider);
for (Method m : this.providerType.getMethods()) {
if (providerMethodName.equals(m.getName())) {
if (m.getParameterTypes().length < 2
&& m.getReturnType() == String.class) {
this.providerMethod = m;
this.providerTakesParameterObject = m.getParameterTypes().length == 1;
}
}
}
} catch (Exception e) {
throw new BuilderException("Error creating SqlSource for SqlProvider. Cause: " + e, e);
}
if (this.providerMethod == null) {
throw new BuilderException("Error creating SqlSource for SqlProvider. Method '"
+ providerMethodName + "' not found in SqlProvider '" + this.providerType.getName() + "'.");
}
}
It turns out that we can pass any number of arguments in methods annotated with SelectProvider (or any other provider). But the method actually providing the query (doSomething, in my case) will actually receive a single argument i.e. a map wrapper around all the arguments. For example, if the arguments were as in the questions above (a set and an integer), we can access them from the map (called parametersMap) as follows :
Set<Integer> nums = (Set<Integer>) parametersMap.get("0");
int groupId = (Integer) parametersMap.get("1");
The first parameter is keyed with "0" and the second with "1" and so on.
IMHO, the arguments should have been keyed with their names so that we could do something like :
parametersMap.get("set");
parametersMap.get("guideId")
It would probably have been more clean. But that's how its implemented.
For providing multiple arguments use #Param tag in the arguments.