Reputation: 77
So i'm getting an error in my program with my Google Speech API. The error is "Not all paths return a value" as the title suggest. Here is the code (2 peices)(Same error)
public static SpeechInputResult ProcessFlacFile(string FlacFileName, int BIT_RATE = DEFAULT_BIT_RATE, string language = DEFAULT_LANGUAGE, uint maxresults = 1)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.google.com/speech-api/v1/recognize?xjerr=1" + "&client=" + client + "&lang=" + language + "&maxresults=" + maxresults + "&pfilter=0");
FileStream fStream = new FileStream(FlacFileName, FileMode.Open, FileAccess.Read);
request.Proxy = null;
request.Timeout = 60000;
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "audio/x-flac; rate=8000";
//bitrate must = .flac file
request.UserAgent = client;
FileInfo fInfo = new FileInfo(FlacFileName);
long numbytes = fInfo.Length;
byte[] data = null;
using (FileStream fstream = new FileStream(FlacFileName, FileMode.Open, FileAccess.Read))
data = new byte[fstream.Length];
fStream.Read(data, 0, Convert.ToInt32(fStream.Length));
fStream.Close();
using (Stream wrStream = request.GetRequestStream())
{
wrStream.Write(data, 0, data.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
dynamic resp = response.GetResponseStream();
if (resp != null)
{
StreamReader sr = new StreamReader(resp);
MessageBox.Show(sr.ReadToEnd());
resp.Close();
resp.Dispose();
}
}
catch (System.Exception ee)
{
MessageBox.Show(ee.Message);
}
}
}
}
and 2nd piece here:
public class Hypothesis
{
public string utterance;
public double confidence = -1.0d;//-1 = No Value
public override string ToString()
{
return "'" +utterance + "'" + ((confidence == -1) ? "" : "@" + confidence);
}
public List<Hypothesis> hypotheses = new List<Hypothesis>();
public Hypothesis getBestHypothesis()
{
if (hypotheses.Count() <=0)
return null;
Hypothesis H = hypotheses[0];
foreach (Hypothesis h in hypotheses)
{
if (h.confidence>=H.confidence)
{
H = h;
}
return H;
}
}
Both code have the same error and it only seems to happen if I a certain variable name to be the same name as another variable (FlacFileName, to be exact). If you guys could tell me why this is happening that would be awesome thanks!
Upvotes: 0
Views: 387
Reputation: 433
ProcessFlacFile
method in the above code should return a type SpeechInputResult
object in all cases.
You can return the object after try and catch as
try
{
}
catch
{
}
return SpeechInputResultObj
or
return SpeechInputResult
object in both try
and catch
statement
try
{
....
return SpeechInputResultObj;
}
catch
{
....
return SpeechInputResultObj;
}
Upvotes: 1
Reputation: 1289
i find that in your code you have to return SpeechInputResult
but there is no where you are using return statement.
It is preferred that you use return SpeechInputResult
in your code
other wise put your function as below if your are not willing to return anything to it.
public static void ProcessFlacFile(string FlacFileName, int BIT_RATE = DEFAULT_BIT_RATE, string language = DEFAULT_LANGUAGE, uint maxresults = 1)
you can use void instead of the class SpeechInputResult
as in the above code if you want to be it without the error in this case.
in the second piece of code you only instantiated the code. ie declared and directly used it you must have something assigned to the object you created so that you can perform operation in the foreach.
in this case hypotheses
doesnt have any thing inside it.
Also you have written return statement inside the foreach loop where as the focus will never go inside the foreach loop.
First you assign something to hypotheses
so that it has value in it and then perform actions.
Upvotes: 2
Reputation: 66449
Like everyone mentioned, add a return
statement to the end of the first method. Just a return null;
or whatever's appropriate in your situation. Actually, you have no other return
statements in that method at all, so it's not a "not all paths return a value" situation like the second... you may just want to change the signature from SpeechInputResult
to void
.
In the second method, it seems like you have your bases covered, because:
null
if hypotheses
is empty andhypotheses
with the largest "confidence" if it's not emptyBut the compiler isn't smart enough to see that. Try moving return null;
to the end. The only way you'll make it there is if there are no elements in the list and the foreach
loop doesn't run.
public Hypothesis getBestHypothesis()
{
if (hypotheses.Any())
{
Hypothesis H = hypotheses[0];
foreach (Hypothesis h in hypotheses)
{
if (h.confidence >= H.confidence)
{
H = h;
}
return H;
}
}
return null;
}
Also, that whole second method could be reduced (with the help of LINQ):
return hypotheses.OrderByDescending(x => x.confidence).FirstOrDefault();
Upvotes: 2
Reputation: 166396
Your method signature
public static SpeechInputResult ProcessFlacFile(string FlacFileName, int BIT_RATE = DEFAULT_BIT_RATE, string language = DEFAULT_LANGUAGE, uint maxresults = 1)
states you will be returning a SpeechInputResult
but you have no return
statement.
You should either change the signature to void
or actually return a value.
In the second instance, you need a return statement after the for loop.
Upvotes: 2
Reputation: 2714
In your first method, you declared in signature that, it will be returning instance of typeSpeechInputResult
, while you are not returning it from body.
And, in second method, you are returning from inside foreach
loop, but what if your hypotheses
list doesn't have any element? You should also be returning either a default value from outside the foreach
loop or you should throw an exception if it isn't your expected behaviour.
Upvotes: 1
Reputation: 72
First block of code does not have any return statement. In the second code block , put a dummy return statement outside the for loop.
Upvotes: 1